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