Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 1 | /* 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 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" |
Harald Welte | 9d90d28 | 2018-06-29 22:25:42 +0200 | [diff] [blame] | 26 | #include <osmocom/core/timer.h> |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 27 | |
Harald Welte | d525231 | 2017-02-27 20:34:20 +0100 | [diff] [blame] | 28 | unsigned int g_unique_id[4]; |
Harald Welte | 8ee15db | 2016-03-20 16:00:39 +0100 | [diff] [blame] | 29 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 30 | /*------------------------------------------------------------------------------ |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 31 | * Internal variables |
| 32 | *------------------------------------------------------------------------------*/ |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 33 | typedef struct { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 34 | /* 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 Welte | 3bafe43 | 2016-03-20 16:43:12 +0100 | [diff] [blame] | 42 | /* Interrupt handler for USART1 */ |
| 43 | void (*usart0_irq) (void); |
| 44 | /* Interrupt handler for USART1 */ |
| 45 | void (*usart1_irq) (void); |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 46 | } conf_func; |
| 47 | |
Harald Welte | d4c1421 | 2015-11-07 18:25:46 +0100 | [diff] [blame] | 48 | static const conf_func config_func_ptrs[] = { |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 49 | /* array slot 0 is empty, usb configs start at 1 */ |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 50 | #ifdef HAVE_SNIFFER |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 51 | [CFG_NUM_SNIFF] = { |
| 52 | .configure = Sniffer_configure, |
| 53 | .init = Sniffer_init, |
| 54 | .exit = Sniffer_exit, |
| 55 | .run = Sniffer_run, |
| 56 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 57 | #endif |
| 58 | #ifdef HAVE_CCID |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 59 | [CFG_NUM_CCID] = { |
| 60 | .configure = CCID_configure, |
| 61 | .init = CCID_init, |
| 62 | .exit = CCID_exit, |
| 63 | .run = CCID_run, |
| 64 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 65 | #endif |
| 66 | #ifdef HAVE_CARDEM |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 67 | [CFG_NUM_PHONE] = { |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 68 | .configure = mode_cardemu_configure, |
| 69 | .init = mode_cardemu_init, |
| 70 | .exit = mode_cardemu_exit, |
| 71 | .run = mode_cardemu_run, |
Harald Welte | 3bafe43 | 2016-03-20 16:43:12 +0100 | [diff] [blame] | 72 | .usart0_irq = mode_cardemu_usart0_irq, |
| 73 | .usart1_irq = mode_cardemu_usart1_irq, |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 74 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 75 | #endif |
| 76 | #ifdef HAVE_MITM |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 77 | [CFG_NUM_MITM] = { |
| 78 | .configure = MITM_configure, |
| 79 | .init = MITM_init, |
| 80 | .exit = MITM_exit, |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 81 | .run = MITM_run, |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 82 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 83 | #endif |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 84 | }; |
| 85 | |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 86 | /*------------------------------------------------------------------------------ |
| 87 | * Internal variables |
| 88 | *------------------------------------------------------------------------------*/ |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 89 | #if defined(HAVE_SNIFFER) |
Harald Welte | 8d6a5d8 | 2015-11-07 18:27:05 +0100 | [diff] [blame] | 90 | static volatile enum confNum simtrace_config = CFG_NUM_SNIFF; |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 91 | #elif defined(HAVE_CARDEM) |
| 92 | static volatile enum confNum simtrace_config = CFG_NUM_PHONE; |
| 93 | #elif defined(HAVE_CCID) |
| 94 | static volatile enum confNum simtrace_config = CFG_NUM_CCID; |
| 95 | #endif |
| 96 | |
Harald Welte | 8d6a5d8 | 2015-11-07 18:27:05 +0100 | [diff] [blame] | 97 | /*---------------------------------------------------------------------------- |
| 98 | * Callbacks |
| 99 | *----------------------------------------------------------------------------*/ |
| 100 | |
| 101 | void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum) |
| 102 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 103 | TRACE_INFO_WP("cfgChanged%d ", cfgnum); |
| 104 | simtrace_config = cfgnum; |
Harald Welte | 8d6a5d8 | 2015-11-07 18:27:05 +0100 | [diff] [blame] | 105 | } |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 106 | |
Harald Welte | 3bafe43 | 2016-03-20 16:43:12 +0100 | [diff] [blame] | 107 | void USART1_IrqHandler(void) |
| 108 | { |
Harald Welte | 2afd57f | 2017-11-28 22:52:56 +0100 | [diff] [blame] | 109 | if (config_func_ptrs[simtrace_config].usart1_irq) |
| 110 | config_func_ptrs[simtrace_config].usart1_irq(); |
Harald Welte | 3bafe43 | 2016-03-20 16:43:12 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void USART0_IrqHandler(void) |
| 114 | { |
Harald Welte | 2afd57f | 2017-11-28 22:52:56 +0100 | [diff] [blame] | 115 | if (config_func_ptrs[simtrace_config].usart0_irq) |
| 116 | config_func_ptrs[simtrace_config].usart0_irq(); |
Harald Welte | 3bafe43 | 2016-03-20 16:43:12 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Harald Welte | f9a182d | 2017-01-11 22:22:16 +0100 | [diff] [blame] | 119 | /* returns '1' in case we should break any endless loop */ |
Harald Welte | af61479 | 2017-01-12 18:59:41 +0100 | [diff] [blame] | 120 | static void check_exec_dbg_cmd(void) |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 121 | { |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 122 | int ch; |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 123 | |
| 124 | if (!UART_IsRxReady()) |
| 125 | return; |
| 126 | |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 127 | ch = UART_GetChar(); |
Harald Welte | 4689345 | 2018-07-03 22:30:30 +0200 | [diff] [blame] | 128 | /* We must echo the character to make python fdexpect happy, whcih we use in factory testing */ |
| 129 | fputc(ch, stdout); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 130 | |
| 131 | board_exec_dbg_cmd(ch); |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 132 | } |
Harald Welte | e974fbb | 2016-10-19 19:36:07 +0200 | [diff] [blame] | 133 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 134 | /*------------------------------------------------------------------------------ |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 135 | * Main |
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 | #define MAX_USB_ITER BOARD_MCK/72 // This should be around a second |
| 138 | extern int main(void) |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 139 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 140 | uint8_t isUsbConnected = 0; |
| 141 | enum confNum last_simtrace_config = simtrace_config; |
| 142 | unsigned int i = 0; |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 143 | |
Harald Welte | abba8a8 | 2017-03-06 16:58:00 +0100 | [diff] [blame] | 144 | led_init(); |
| 145 | led_blink(LED_RED, BLINK_3O_5F); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 146 | |
Kévin Redon | d8ebd6a | 2018-07-28 19:04:23 +0200 | [diff] [blame] | 147 | /* Enable watchdog for 2000ms, with no window */ |
Harald Welte | 45ebe45 | 2017-03-03 19:01:53 +0100 | [diff] [blame] | 148 | WDT_Enable(WDT, WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT | WDT_MR_WDIDLEHLT | |
Kévin Redon | d8ebd6a | 2018-07-28 19:04:23 +0200 | [diff] [blame] | 149 | (WDT_GetPeriod(2000) << 16) | WDT_GetPeriod(2000)); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 150 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 151 | PIO_InitializeInterrupts(0); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 152 | |
Harald Welte | 8ee15db | 2016-03-20 16:00:39 +0100 | [diff] [blame] | 153 | EEFC_ReadUniqueID(g_unique_id); |
| 154 | |
Kévin Redon | 33d1eb7 | 2018-07-08 13:58:12 +0200 | [diff] [blame] | 155 | printf("\n\r\n\r" |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 156 | "=============================================================================\n\r" |
| 157 | "SIMtrace2 firmware " GIT_VERSION " (C) 2010-2016 by Harald Welte\n\r" |
| 158 | "=============================================================================\n\r"); |
Harald Welte | 2315e6b | 2016-03-19 21:37:55 +0100 | [diff] [blame] | 159 | |
Kévin Redon | 738a04a | 2018-07-28 19:02:54 +0200 | [diff] [blame] | 160 | #if (TRACE_LEVEL >= TRACE_LEVEL_INFO) |
Harald Welte | 7214b47 | 2017-03-03 19:02:09 +0100 | [diff] [blame] | 161 | TRACE_INFO("Chip ID: 0x%08x (Ext 0x%08x)\n\r", CHIPID->CHIPID_CIDR, CHIPID->CHIPID_EXID); |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 162 | TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\n\r", |
Harald Welte | 8ee15db | 2016-03-20 16:00:39 +0100 | [diff] [blame] | 163 | g_unique_id[0], g_unique_id[1], |
| 164 | g_unique_id[2], g_unique_id[3]); |
Kévin Redon | 738a04a | 2018-07-28 19:02:54 +0200 | [diff] [blame] | 165 | uint8_t reset_cause = (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> RSTC_SR_RSTTYP_Pos; |
| 166 | static const char* reset_causes[] = { |
| 167 | "general reset (first power-up reset)", |
| 168 | "backup reset (return from backup mode)", |
| 169 | "watchdog reset (watchdog fault occurred)", |
| 170 | "software reset (processor reset required by the software)", |
| 171 | "user reset (NRST pin detected low)", |
| 172 | }; |
| 173 | if (reset_cause < ARRAY_SIZE(reset_causes)) { |
| 174 | TRACE_INFO("Reset Cause: %s\n\r", reset_causes[reset_cause]); |
| 175 | } else { |
| 176 | TRACE_INFO("Reset Cause: 0x%x\n\r", (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> RSTC_SR_RSTTYP_Pos); |
| 177 | } |
| 178 | #endif |
Harald Welte | 8ee15db | 2016-03-20 16:00:39 +0100 | [diff] [blame] | 179 | |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 180 | board_main_top(); |
Harald Welte | 6732248 | 2017-02-04 14:43:41 +0100 | [diff] [blame] | 181 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 182 | TRACE_INFO("USB init...\n\r"); |
Harald Welte | e07aed6 | 2016-12-22 22:32:15 +0100 | [diff] [blame] | 183 | SIMtrace_USB_Initialize(); |
| 184 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 185 | while (USBD_GetState() < USBD_STATE_CONFIGURED) { |
Harald Welte | 45ebe45 | 2017-03-03 19:01:53 +0100 | [diff] [blame] | 186 | WDT_Restart(WDT); |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 187 | check_exec_dbg_cmd(); |
Harald Welte | 67415e3 | 2016-09-01 20:57:56 +0200 | [diff] [blame] | 188 | #if 0 |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 189 | if (i >= MAX_USB_ITER * 3) { |
| 190 | TRACE_ERROR("Resetting board (USB could " |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 191 | "not be configured)\n\r"); |
Harald Welte | f415d71 | 2017-03-03 01:51:43 +0100 | [diff] [blame] | 192 | USBD_Disconnect(); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 193 | NVIC_SystemReset(); |
| 194 | } |
Harald Welte | 67415e3 | 2016-09-01 20:57:56 +0200 | [diff] [blame] | 195 | #endif |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 196 | i++; |
| 197 | } |
Christina Quast | de68867 | 2015-04-12 15:20:57 +0200 | [diff] [blame] | 198 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 199 | TRACE_INFO("calling configure of all configurations...\n\r"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 200 | for (i = 1; i < sizeof(config_func_ptrs) / sizeof(config_func_ptrs[0]); |
| 201 | ++i) { |
| 202 | if (config_func_ptrs[i].configure) |
| 203 | config_func_ptrs[i].configure(); |
| 204 | } |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 205 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 206 | TRACE_INFO("calling init of config %u...\n\r", simtrace_config); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 207 | config_func_ptrs[simtrace_config].init(); |
| 208 | last_simtrace_config = simtrace_config; |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 209 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 210 | TRACE_INFO("entering main loop...\n\r"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 211 | while (1) { |
Harald Welte | 45ebe45 | 2017-03-03 19:01:53 +0100 | [diff] [blame] | 212 | WDT_Restart(WDT); |
Harald Welte | 04e37a8 | 2016-03-20 15:15:34 +0100 | [diff] [blame] | 213 | #if TRACE_LEVEL >= TRACE_LEVEL_DEBUG |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 214 | const char rotor[] = { '-', '\\', '|', '/' }; |
| 215 | putchar('\b'); |
| 216 | putchar(rotor[i++ % ARRAY_SIZE(rotor)]); |
Harald Welte | 04e37a8 | 2016-03-20 15:15:34 +0100 | [diff] [blame] | 217 | #endif |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 218 | check_exec_dbg_cmd(); |
Harald Welte | 987f59a | 2017-02-04 12:34:35 +0100 | [diff] [blame] | 219 | osmo_timers_prepare(); |
| 220 | osmo_timers_update(); |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 221 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 222 | if (USBD_GetState() < USBD_STATE_CONFIGURED) { |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 223 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 224 | if (isUsbConnected) { |
| 225 | isUsbConnected = 0; |
| 226 | } |
| 227 | } else if (isUsbConnected == 0) { |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 228 | TRACE_INFO("USB is now configured\n\r"); |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 229 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 230 | isUsbConnected = 1; |
| 231 | } |
| 232 | if (last_simtrace_config != simtrace_config) { |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 233 | TRACE_INFO("USB config chg %u -> %u\n\r", |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 234 | last_simtrace_config, simtrace_config); |
| 235 | config_func_ptrs[last_simtrace_config].exit(); |
| 236 | config_func_ptrs[simtrace_config].init(); |
| 237 | last_simtrace_config = simtrace_config; |
| 238 | } else { |
| 239 | config_func_ptrs[simtrace_config].run(); |
| 240 | } |
| 241 | } |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 242 | } |