Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 1 | /* 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 Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 19 | /*------------------------------------------------------------------------------ |
| 20 | * Headers |
| 21 | *------------------------------------------------------------------------------*/ |
| 22 | |
| 23 | #include "board.h" |
Harald Welte | 1605564 | 2016-03-03 11:02:45 +0100 | [diff] [blame] | 24 | #include "simtrace.h" |
Harald Welte | ebbb645 | 2016-02-29 17:57:51 +0100 | [diff] [blame] | 25 | #include "utils.h" |
| 26 | #include "req_ctx.h" |
Harald Welte | 9d90d28 | 2018-06-29 22:25:42 +0200 | [diff] [blame] | 27 | #include <osmocom/core/timer.h> |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 28 | |
Harald Welte | d525231 | 2017-02-27 20:34:20 +0100 | [diff] [blame] | 29 | unsigned int g_unique_id[4]; |
Harald Welte | 8ee15db | 2016-03-20 16:00:39 +0100 | [diff] [blame] | 30 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 31 | /*------------------------------------------------------------------------------ |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 32 | * Internal variables |
| 33 | *------------------------------------------------------------------------------*/ |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 34 | typedef struct { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 35 | /* 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 Welte | 3bafe43 | 2016-03-20 16:43:12 +0100 | [diff] [blame] | 43 | /* Interrupt handler for USART1 */ |
| 44 | void (*usart0_irq) (void); |
| 45 | /* Interrupt handler for USART1 */ |
| 46 | void (*usart1_irq) (void); |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 47 | } conf_func; |
| 48 | |
Harald Welte | d4c1421 | 2015-11-07 18:25:46 +0100 | [diff] [blame] | 49 | static const conf_func config_func_ptrs[] = { |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 50 | /* array slot 0 is empty, usb configs start at 1 */ |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 51 | #ifdef HAVE_SNIFFER |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 52 | [CFG_NUM_SNIFF] = { |
| 53 | .configure = Sniffer_configure, |
| 54 | .init = Sniffer_init, |
| 55 | .exit = Sniffer_exit, |
| 56 | .run = Sniffer_run, |
| 57 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 58 | #endif |
| 59 | #ifdef HAVE_CCID |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 60 | [CFG_NUM_CCID] = { |
| 61 | .configure = CCID_configure, |
| 62 | .init = CCID_init, |
| 63 | .exit = CCID_exit, |
| 64 | .run = CCID_run, |
| 65 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 66 | #endif |
| 67 | #ifdef HAVE_CARDEM |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 68 | [CFG_NUM_PHONE] = { |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 69 | .configure = mode_cardemu_configure, |
| 70 | .init = mode_cardemu_init, |
| 71 | .exit = mode_cardemu_exit, |
| 72 | .run = mode_cardemu_run, |
Harald Welte | 3bafe43 | 2016-03-20 16:43:12 +0100 | [diff] [blame] | 73 | .usart0_irq = mode_cardemu_usart0_irq, |
| 74 | .usart1_irq = mode_cardemu_usart1_irq, |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 75 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 76 | #endif |
| 77 | #ifdef HAVE_MITM |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 78 | [CFG_NUM_MITM] = { |
| 79 | .configure = MITM_configure, |
| 80 | .init = MITM_init, |
| 81 | .exit = MITM_exit, |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 82 | .run = MITM_run, |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 83 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 84 | #endif |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 85 | }; |
| 86 | |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 87 | /*------------------------------------------------------------------------------ |
| 88 | * Internal variables |
| 89 | *------------------------------------------------------------------------------*/ |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 90 | #if defined(HAVE_SNIFFER) |
Harald Welte | 8d6a5d8 | 2015-11-07 18:27:05 +0100 | [diff] [blame] | 91 | static volatile enum confNum simtrace_config = CFG_NUM_SNIFF; |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 92 | #elif defined(HAVE_CARDEM) |
| 93 | static volatile enum confNum simtrace_config = CFG_NUM_PHONE; |
| 94 | #elif defined(HAVE_CCID) |
| 95 | static volatile enum confNum simtrace_config = CFG_NUM_CCID; |
| 96 | #endif |
| 97 | |
Harald Welte | 8d6a5d8 | 2015-11-07 18:27:05 +0100 | [diff] [blame] | 98 | /*---------------------------------------------------------------------------- |
| 99 | * Callbacks |
| 100 | *----------------------------------------------------------------------------*/ |
| 101 | |
| 102 | void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum) |
| 103 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 104 | TRACE_INFO_WP("cfgChanged%d ", cfgnum); |
| 105 | simtrace_config = cfgnum; |
Harald Welte | 8d6a5d8 | 2015-11-07 18:27:05 +0100 | [diff] [blame] | 106 | } |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 107 | |
Harald Welte | 3bafe43 | 2016-03-20 16:43:12 +0100 | [diff] [blame] | 108 | void USART1_IrqHandler(void) |
| 109 | { |
| 110 | config_func_ptrs[simtrace_config].usart1_irq(); |
| 111 | } |
| 112 | |
| 113 | void USART0_IrqHandler(void) |
| 114 | { |
| 115 | config_func_ptrs[simtrace_config].usart0_irq(); |
| 116 | } |
| 117 | |
Harald Welte | f9a182d | 2017-01-11 22:22:16 +0100 | [diff] [blame] | 118 | /* returns '1' in case we should break any endless loop */ |
Harald Welte | af61479 | 2017-01-12 18:59:41 +0100 | [diff] [blame] | 119 | static void check_exec_dbg_cmd(void) |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 120 | { |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 121 | int ch; |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 122 | |
| 123 | if (!UART_IsRxReady()) |
| 124 | return; |
| 125 | |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 126 | ch = UART_GetChar(); |
| 127 | |
| 128 | board_exec_dbg_cmd(ch); |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 129 | } |
Harald Welte | e974fbb | 2016-10-19 19:36:07 +0200 | [diff] [blame] | 130 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 131 | /*------------------------------------------------------------------------------ |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 132 | * Main |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 133 | *------------------------------------------------------------------------------*/ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 134 | #define MAX_USB_ITER BOARD_MCK/72 // This should be around a second |
| 135 | extern int main(void) |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 136 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 137 | uint8_t isUsbConnected = 0; |
| 138 | enum confNum last_simtrace_config = simtrace_config; |
| 139 | unsigned int i = 0; |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 140 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 141 | LED_Configure(LED_NUM_RED); |
| 142 | LED_Configure(LED_NUM_GREEN); |
| 143 | LED_Set(LED_NUM_RED); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 144 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 145 | /* Disable watchdog */ |
| 146 | WDT_Disable(WDT); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 147 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 148 | req_ctx_init(); |
Harald Welte | ebbb645 | 2016-02-29 17:57:51 +0100 | [diff] [blame] | 149 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 150 | PIO_InitializeInterrupts(0); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 151 | |
Harald Welte | 8ee15db | 2016-03-20 16:00:39 +0100 | [diff] [blame] | 152 | EEFC_ReadUniqueID(g_unique_id); |
| 153 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 154 | printf("\r\n\r\n" |
Harald Welte | 2315e6b | 2016-03-19 21:37:55 +0100 | [diff] [blame] | 155 | "=============================================================================\r\n" |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 156 | "SIMtrace2 firmware " GIT_REVISION " (C) 2010-2017 by Harald Welte\r\n" |
Harald Welte | 2315e6b | 2016-03-19 21:37:55 +0100 | [diff] [blame] | 157 | "=============================================================================\r\n"); |
| 158 | |
Harald Welte | 8ee15db | 2016-03-20 16:00:39 +0100 | [diff] [blame] | 159 | 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 Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 163 | board_main_top(); |
Harald Welte | 6732248 | 2017-02-04 14:43:41 +0100 | [diff] [blame] | 164 | |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 165 | TRACE_INFO("USB init...\r\n"); |
Harald Welte | e07aed6 | 2016-12-22 22:32:15 +0100 | [diff] [blame] | 166 | SIMtrace_USB_Initialize(); |
| 167 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 168 | while (USBD_GetState() < USBD_STATE_CONFIGURED) { |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 169 | check_exec_dbg_cmd(); |
Harald Welte | 67415e3 | 2016-09-01 20:57:56 +0200 | [diff] [blame] | 170 | #if 0 |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 171 | if (i >= MAX_USB_ITER * 3) { |
| 172 | TRACE_ERROR("Resetting board (USB could " |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 173 | "not be configured)\r\n"); |
Harald Welte | f415d71 | 2017-03-03 01:51:43 +0100 | [diff] [blame] | 174 | USBD_Disconnect(); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 175 | NVIC_SystemReset(); |
| 176 | } |
Harald Welte | 67415e3 | 2016-09-01 20:57:56 +0200 | [diff] [blame] | 177 | #endif |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 178 | i++; |
| 179 | } |
Christina Quast | de68867 | 2015-04-12 15:20:57 +0200 | [diff] [blame] | 180 | |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 181 | TRACE_INFO("calling configure of all configurations...\r\n"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 182 | 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 Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 187 | |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 188 | TRACE_INFO("calling init of config %u...\r\n", simtrace_config); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 189 | config_func_ptrs[simtrace_config].init(); |
| 190 | last_simtrace_config = simtrace_config; |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 191 | |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 192 | TRACE_INFO("entering main loop...\r\n"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 193 | while (1) { |
Harald Welte | 04e37a8 | 2016-03-20 15:15:34 +0100 | [diff] [blame] | 194 | #if TRACE_LEVEL >= TRACE_LEVEL_DEBUG |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 195 | const char rotor[] = { '-', '\\', '|', '/' }; |
| 196 | putchar('\b'); |
| 197 | putchar(rotor[i++ % ARRAY_SIZE(rotor)]); |
Harald Welte | 04e37a8 | 2016-03-20 15:15:34 +0100 | [diff] [blame] | 198 | #endif |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 199 | check_exec_dbg_cmd(); |
Harald Welte | 987f59a | 2017-02-04 12:34:35 +0100 | [diff] [blame] | 200 | osmo_timers_prepare(); |
| 201 | osmo_timers_update(); |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 202 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 203 | if (USBD_GetState() < USBD_STATE_CONFIGURED) { |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 204 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 205 | if (isUsbConnected) { |
| 206 | isUsbConnected = 0; |
| 207 | } |
| 208 | } else if (isUsbConnected == 0) { |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 209 | TRACE_INFO("USB is now configured\r\n"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 210 | LED_Set(LED_NUM_GREEN); |
| 211 | LED_Clear(LED_NUM_RED); |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 212 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 213 | 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 Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 225 | } |