Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 1 | /* SIMtrace 2 sniffer mode |
| 2 | * |
| 3 | * (C) 2016-2017 by Harald Welte <hwelte@hmw-consulting.de> |
| 4 | * (C) 2018 by sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de> |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 5 | * |
Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify |
Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 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 |
Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 9 | * (at your option) any later version. |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 10 | * |
Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 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 |
Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 14 | * GNU General Public License for more details. |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 15 | * |
Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 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 |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 19 | */ |
Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 20 | /* This code implement the Sniffer mode to sniff the communication between a |
| 21 | * SIM card (or any ISO 7816 smart card) and a phone (or any ISO 7816 card |
| 22 | * reader). |
| 23 | * For historical reasons (i.e. SIMtrace hardware) the USART peripheral |
| 24 | * connected to the SIM card is used. |
| 25 | * TODO put common ISO7816-3 code is separate library (and combine clean with |
| 26 | * iso7816_4) |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 27 | */ |
Kévin Redon | 36abece | 2018-06-04 16:30:01 +0200 | [diff] [blame] | 28 | #include "board.h" |
| 29 | #include "simtrace.h" |
| 30 | |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 31 | #ifdef HAVE_SNIFFER |
| 32 | |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 33 | /*------------------------------------------------------------------------------ |
| 34 | * Headers |
| 35 | *------------------------------------------------------------------------------*/ |
| 36 | |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 37 | #include <string.h> |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 38 | #include "utils.h" |
| 39 | #include "iso7816_fidi.h" |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 40 | /* USB related libraries */ |
| 41 | #include "osmocom/core/linuxlist.h" |
| 42 | #include "osmocom/core/msgb.h" |
| 43 | #include "llist_irqsafe.h" |
| 44 | #include "usb_buf.h" |
| 45 | #include "simtrace_usb.h" |
| 46 | #include "simtrace_prot.h" |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 47 | |
| 48 | /*------------------------------------------------------------------------------ |
| 49 | * Internal definitions |
| 50 | *------------------------------------------------------------------------------*/ |
| 51 | |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 52 | /*! Maximum Answer-To-Reset (ATR) size in bytes |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 53 | * @note defined in ISO/IEC 7816-3:2006(E) section 8.2.1 as 32, on top the initial character TS of section 8.1 |
| 54 | * @remark technical there is no size limitation since Yi present in T0,TDi will indicate if more interface bytes are present, including TDi+i |
| 55 | */ |
| 56 | #define MAX_ATR_SIZE 33 |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 57 | /*! Maximum Protocol and Parameters Selection (PPS) size in bytes |
| 58 | * @note defined in ISO/IEC 7816-3:2006(E) section 9.2 |
| 59 | */ |
| 60 | #define MAX_PPS_SIZE 6 |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 61 | |
| 62 | /*! ISO 7816-3 states relevant to the sniff mode */ |
| 63 | enum iso7816_3_sniff_state { |
| 64 | ISO7816_S_RESET, /*!< in Reset */ |
| 65 | ISO7816_S_WAIT_ATR, /*!< waiting for ATR to start */ |
| 66 | ISO7816_S_IN_ATR, /*!< while we are receiving the ATR */ |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 67 | ISO7816_S_WAIT_TPDU, /*!< waiting for start of new TPDU */ |
| 68 | ISO7816_S_IN_TPDU, /*!< inside a single TPDU */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 69 | ISO7816_S_IN_PPS_REQ, /*!< while we are inside the PPS request */ |
| 70 | ISO7816_S_WAIT_PPS_RSP, /*!< waiting for start of the PPS response */ |
| 71 | ISO7816_S_IN_PPS_RSP, /*!< while we are inside the PPS request */ |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | /*! Answer-To-Reset (ATR) sub-states of ISO7816_S_IN_ATR |
| 75 | * @note defined in ISO/IEC 7816-3:2006(E) section 8 |
| 76 | */ |
| 77 | enum atr_sniff_state { |
| 78 | ATR_S_WAIT_TS, /*!< initial byte */ |
| 79 | ATR_S_WAIT_T0, /*!< format byte */ |
| 80 | ATR_S_WAIT_TA, /*!< first sub-group interface byte */ |
| 81 | ATR_S_WAIT_TB, /*!< second sub-group interface byte */ |
| 82 | ATR_S_WAIT_TC, /*!< third sub-group interface byte */ |
| 83 | ATR_S_WAIT_TD, /*!< fourth sub-group interface byte */ |
| 84 | ATR_S_WAIT_HIST, /*!< historical byte */ |
| 85 | ATR_S_WAIT_TCK, /*!< check byte */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | /*! Protocol and Parameters Selection (PPS) sub-states of ISO7816_S_IN_PTS_REQ/ISO7816_S_IN_PTS_RSP |
| 89 | * @note defined in ISO/IEC 7816-3:2006(E) section 9 |
| 90 | */ |
| 91 | enum pps_sniff_state { |
| 92 | PPS_S_WAIT_PPSS, /*!< initial byte */ |
| 93 | PPS_S_WAIT_PPS0, /*!< format byte */ |
| 94 | PPS_S_WAIT_PPS1, /*!< first parameter byte */ |
| 95 | PPS_S_WAIT_PPS2, /*!< second parameter byte */ |
| 96 | PPS_S_WAIT_PPS3, /*!< third parameter byte */ |
| 97 | PPS_S_WAIT_PCK, /*!< check byte */ |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 98 | PPS_S_WAIT_END, /*!< all done */ |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 99 | }; |
Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 100 | |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 101 | /*! Transport Protocol Data Unit (TPDU) sub-states of ISO7816_S_IN_TPDU |
| 102 | * @note defined in ISO/IEC 7816-3:2006(E) section 10 and 12 |
| 103 | * @remark APDUs are formed by one or more command+response TPDUs |
| 104 | */ |
| 105 | enum tpdu_sniff_state { |
| 106 | TPDU_S_CLA, /*!< class byte */ |
| 107 | TPDU_S_INS, /*!< instruction byte */ |
| 108 | TPDU_S_P1, /*!< first parameter byte for the instruction */ |
| 109 | TPDU_S_P2, /*!< second parameter byte for the instruction */ |
| 110 | TPDU_S_P3, /*!< third parameter byte encoding the data length */ |
| 111 | TPDU_S_PROCEDURE, /*!< procedure byte (could also be SW1) */ |
| 112 | TPDU_S_DATA_REMAINING, /*!< remaining data bytes */ |
| 113 | TPDU_S_DATA_SINGLE, /*!< single data byte */ |
| 114 | TPDU_S_SW1, /*!< first status word */ |
| 115 | TPDU_S_SW2, /*!< second status word */ |
| 116 | }; |
| 117 | |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 118 | /*------------------------------------------------------------------------------ |
| 119 | * Internal variables |
| 120 | *------------------------------------------------------------------------------*/ |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 121 | |
| 122 | /* note: the sniffer code is currently designed to support only one sniffing interface, but the hardware would support a second one. |
| 123 | * to support a second sniffer interface the code should be restructured to use handles. |
| 124 | */ |
| 125 | /* Pin configurations */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 126 | /*! Pin configuration to sniff communication (using USART connection card) */ |
Kévin Redon | ee62a9d | 2018-06-11 13:42:23 +0200 | [diff] [blame] | 127 | static const Pin pins_sniff[] = { PINS_SIM_SNIFF }; |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 128 | /*! Pin configuration to interconnect phone and card using the bus switch */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 129 | static const Pin pins_bus[] = { PINS_BUS_SNIFF }; |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 130 | /*! Pin configuration to power the card by the phone */ |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 131 | static const Pin pins_power[] = { PINS_PWR_SNIFF }; |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 132 | /*! Pin configuration for timer counter to measure ETU timing */ |
Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 133 | static const Pin pins_tc[] = { PINS_TC }; |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 134 | /*! Pin configuration for card reset line */ |
| 135 | static const Pin pin_rst = PIN_SIM_RST_SNIFF; |
| 136 | |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 137 | /* USART related variables */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 138 | /*! USART peripheral used to sniff communication */ |
Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 139 | static struct Usart_info sniff_usart = { |
| 140 | .base = USART_SIM, |
| 141 | .id = ID_USART_SIM, |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 142 | .state = USART_RCV, |
| 143 | }; |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 144 | /*! Ring buffer to store sniffer communication data */ |
Kévin Redon | 7b73462 | 2018-06-06 16:13:48 +0200 | [diff] [blame] | 145 | static struct ringbuf sniff_buffer; |
| 146 | |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 147 | /* Flags to know is the card status changed (see SIMTRACE_MSGT_DT_SNIFF_CHANGE flags) */ |
| 148 | volatile uint32_t change_flags = 0; |
| 149 | |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 150 | /* ISO 7816 variables */ |
| 151 | /*! ISO 7816-3 state */ |
| 152 | enum iso7816_3_sniff_state iso_state = ISO7816_S_RESET; |
| 153 | /*! ATR state */ |
| 154 | enum atr_sniff_state atr_state; |
| 155 | /*! ATR data |
| 156 | * @remark can be used to check later protocol changes |
| 157 | */ |
| 158 | uint8_t atr[MAX_ATR_SIZE]; |
| 159 | /*! Current index in the ATR data */ |
| 160 | uint8_t atr_i = 0; |
| 161 | /*! If convention conversion is needed */ |
| 162 | bool convention_convert = false; |
| 163 | /*! The supported T protocols */ |
| 164 | uint16_t t_protocol_support = 0; |
| 165 | /*! PPS state |
| 166 | * @remark it is shared between request and response since they aren't simultaneous but follow the same procedure |
| 167 | */ |
| 168 | enum pps_sniff_state pps_state; |
| 169 | /*! PPS request data |
| 170 | * @remark can be used to check PPS response |
| 171 | */ |
| 172 | uint8_t pps_req[MAX_PPS_SIZE]; |
| 173 | /*! PPS response data */ |
| 174 | uint8_t pps_rsp[MAX_PPS_SIZE]; |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 175 | /*! TPDU state */ |
| 176 | enum tpdu_sniff_state tpdu_state; |
| 177 | /*! Final TPDU packet |
| 178 | * @note this is the complete command+response TPDU, including header, data, and status words |
| 179 | * @remark this does not include the procedure bytes |
| 180 | */ |
| 181 | uint8_t tpdu_packet[5+256+2]; |
| 182 | /*! Current index in TPDU packet */ |
Kévin Redon | c9bd715 | 2018-07-03 16:17:00 +0200 | [diff] [blame] | 183 | uint16_t tpdu_packet_i = 0; |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 184 | |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame] | 185 | /*! Waiting Time (WT) |
| 186 | * @note defined in ISO/IEC 7816-3:2006(E) section 8.1 and 10.2 |
| 187 | */ |
| 188 | uint32_t wt = 9600; |
| 189 | |
Kévin Redon | 7b73462 | 2018-06-06 16:13:48 +0200 | [diff] [blame] | 190 | /*------------------------------------------------------------------------------ |
Kévin Redon | 7b73462 | 2018-06-06 16:13:48 +0200 | [diff] [blame] | 191 | * Internal functions |
| 192 | *------------------------------------------------------------------------------*/ |
Kévin Redon | 36abece | 2018-06-04 16:30:01 +0200 | [diff] [blame] | 193 | |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 194 | /*! Convert data between direct and inverse convention |
| 195 | * @note direct convention is LSb first and HIGH=1; inverse conversion in MSb first and LOW=1 |
| 196 | * @remark use a look up table to speed up conversion |
| 197 | */ |
| 198 | static const uint8_t convention_convert_lut[256] = { 0xff, 0x7f, 0xbf, 0x3f, 0xdf, 0x5f, 0x9f, 0x1f, 0xef, 0x6f, 0xaf, 0x2f, 0xcf, 0x4f, 0x8f, 0x0f, 0xf7, 0x77, 0xb7, 0x37, 0xd7, 0x57, 0x97, 0x17, 0xe7, 0x67, 0xa7, 0x27, 0xc7, 0x47, 0x87, 0x07, 0xfb, 0x7b, 0xbb, 0x3b, 0xdb, 0x5b, 0x9b, 0x1b, 0xeb, 0x6b, 0xab, 0x2b, 0xcb, 0x4b, 0x8b, 0x0b, 0xf3, 0x73, 0xb3, 0x33, 0xd3, 0x53, 0x93, 0x13, 0xe3, 0x63, 0xa3, 0x23, 0xc3, 0x43, 0x83, 0x03, 0xfd, 0x7d, 0xbd, 0x3d, 0xdd, 0x5d, 0x9d, 0x1d, 0xed, 0x6d, 0xad, 0x2d, 0xcd, 0x4d, 0x8d, 0x0d, 0xf5, 0x75, 0xb5, 0x35, 0xd5, 0x55, 0x95, 0x15, 0xe5, 0x65, 0xa5, 0x25, 0xc5, 0x45, 0x85, 0x05, 0xf9, 0x79, 0xb9, 0x39, 0xd9, 0x59, 0x99, 0x19, 0xe9, 0x69, 0xa9, 0x29, 0xc9, 0x49, 0x89, 0x09, 0xf1, 0x71, 0xb1, 0x31, 0xd1, 0x51, 0x91, 0x11, 0xe1, 0x61, 0xa1, 0x21, 0xc1, 0x41, 0x81, 0x01, 0xfe, 0x7e, 0xbe, 0x3e, 0xde, 0x5e, 0x9e, 0x1e, 0xee, 0x6e, 0xae, 0x2e, 0xce, 0x4e, 0x8e, 0x0e, 0xf6, 0x76, 0xb6, 0x36, 0xd6, 0x56, 0x96, 0x16, 0xe6, 0x66, 0xa6, 0x26, 0xc6, 0x46, 0x86, 0x06, 0xfa, 0x7a, 0xba, 0x3a, 0xda, 0x5a, 0x9a, 0x1a, 0xea, 0x6a, 0xaa, 0x2a, 0xca, 0x4a, 0x8a, 0x0a, 0xf2, 0x72, 0xb2, 0x32, 0xd2, 0x52, 0x92, 0x12, 0xe2, 0x62, 0xa2, 0x22, 0xc2, 0x42, 0x82, 0x02, 0xfc, 0x7c, 0xbc, 0x3c, 0xdc, 0x5c, 0x9c, 0x1c, 0xec, 0x6c, 0xac, 0x2c, 0xcc, 0x4c, 0x8c, 0x0c, 0xf4, 0x74, 0xb4, 0x34, 0xd4, 0x54, 0x94, 0x14, 0xe4, 0x64, 0xa4, 0x24, 0xc4, 0x44, 0x84, 0x04, 0xf8, 0x78, 0xb8, 0x38, 0xd8, 0x58, 0x98, 0x18, 0xe8, 0x68, 0xa8, 0x28, 0xc8, 0x48, 0x88, 0x08, 0xf0, 0x70, 0xb0, 0x30, 0xd0, 0x50, 0x90, 0x10, 0xe0, 0x60, 0xa0, 0x20, 0xc0, 0x40, 0x80, 0x00, }; |
| 199 | |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame] | 200 | /*! Update Waiting Time (WT) |
| 201 | * @param[in] wi Waiting Integer (0 if unchanged) |
| 202 | * @param[in] d Baud Rate divider (0 if unchanged) |
| 203 | * @note set wt to be used by the receiver timeout |
| 204 | * @note defined in ISO/IEC 7816-3:2006(E) section 8.1 and 10.2 |
| 205 | */ |
| 206 | static void update_wt(uint8_t wi, uint8_t d) |
| 207 | { |
| 208 | static uint8_t wt_wi = 10; /* Waiting time Integer (WI), used to calculate the Waiting Time (WT) */ |
| 209 | static uint8_t wt_d = 1; /* baud rate adjustment integer (the actual value, not the table index) */ |
| 210 | |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 211 | if (0 != wi) { |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame] | 212 | wt_wi = wi; |
| 213 | } |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 214 | if (0 != d) { |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame] | 215 | wt_d = d; |
| 216 | } |
| 217 | wt = wt_wi*960UL*wt_d; |
| 218 | TRACE_INFO("WT updated to %u\n\r", wt); |
| 219 | } |
| 220 | |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 221 | /*! Allocate USB buffer and push + initialize simtrace_msg_hdr |
| 222 | * @param[in] ep USB IN endpoint where the message will be sent to |
| 223 | * @param[in] msg_class SIMtrace USB message class |
| 224 | * @param[in] msg_type SIMtrace USB message type |
| 225 | * @return USB message with allocated ans initialized header, or NULL if allocation failed |
| 226 | */ |
| 227 | static struct msgb *usb_msg_alloc_hdr(uint8_t ep, uint8_t msg_class, uint8_t msg_type) |
| 228 | { |
Kévin Redon | b37bda0 | 2018-07-08 17:34:20 +0200 | [diff] [blame] | 229 | /* Only allocate message if not too many are already in the queue */ |
| 230 | struct llist_head *head = usb_get_queue(SIMTRACE_USB_EP_CARD_DATAIN); |
| 231 | if (!head) { |
| 232 | return NULL; |
| 233 | } |
| 234 | if (llist_count(head) > 5) { |
| 235 | return NULL; |
| 236 | } |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 237 | struct msgb *usb_msg = usb_buf_alloc(SIMTRACE_USB_EP_CARD_DATAIN); |
| 238 | if (!usb_msg) { |
| 239 | return NULL; |
| 240 | } |
| 241 | struct simtrace_msg_hdr *usb_msg_header; |
| 242 | usb_msg->l1h = msgb_put(usb_msg, sizeof(*usb_msg_header)); |
| 243 | usb_msg_header = (struct simtrace_msg_hdr *) usb_msg->l1h; |
| 244 | memset(usb_msg_header, 0, sizeof(*usb_msg_header)); |
Kévin Redon | f82f0f6 | 2018-07-08 15:10:23 +0200 | [diff] [blame] | 245 | usb_msg_header->msg_class = msg_class; |
| 246 | usb_msg_header->msg_type = msg_type; |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 247 | usb_msg->l2h = usb_msg->l1h + sizeof(*usb_msg_header); |
| 248 | |
| 249 | return usb_msg; |
| 250 | } |
| 251 | |
| 252 | /* update SIMtrace header msg_len and submit USB buffer |
| 253 | * param[in] usb_msg USB message to update and send |
| 254 | */ |
| 255 | void usb_msg_upd_len_and_submit(struct msgb *usb_msg) |
| 256 | { |
| 257 | struct simtrace_msg_hdr *usb_msg_header = (struct simtrace_msg_hdr *) usb_msg->l1h; |
| 258 | usb_msg_header->msg_len = msgb_length(usb_msg); |
| 259 | usb_buf_submit(usb_msg); |
| 260 | } |
| 261 | |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 262 | /*! Update the ISO 7816-3 state |
| 263 | * @param[in] iso_state_new new ISO 7816-3 state to update to |
| 264 | */ |
| 265 | static void change_state(enum iso7816_3_sniff_state iso_state_new) |
| 266 | { |
| 267 | /* sanity check */ |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 268 | if (iso_state_new == iso_state) { |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 269 | TRACE_WARNING("Already in ISO 7816 state %u\n\r", iso_state); |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | /* handle actions to perform when switching state */ |
| 274 | switch (iso_state_new) { |
| 275 | case ISO7816_S_RESET: |
| 276 | update_fidi(&sniff_usart, 0x11); /* reset baud rate to default Di/Fi values */ |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame] | 277 | update_wt(10, 1); /* reset WT time-out */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 278 | break; |
| 279 | case ISO7816_S_WAIT_ATR: |
| 280 | rbuf_reset(&sniff_buffer); /* reset buffer for new communication */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 281 | break; |
| 282 | case ISO7816_S_IN_ATR: |
| 283 | atr_i = 0; |
| 284 | convention_convert = false; |
| 285 | t_protocol_support = 0; |
| 286 | atr_state = ATR_S_WAIT_TS; |
| 287 | break; |
| 288 | case ISO7816_S_IN_PPS_REQ: |
| 289 | case ISO7816_S_IN_PPS_RSP: |
| 290 | pps_state = PPS_S_WAIT_PPSS; |
| 291 | break; |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 292 | case ISO7816_S_WAIT_TPDU: |
| 293 | tpdu_state = TPDU_S_CLA; |
| 294 | tpdu_packet_i = 0; |
| 295 | break; |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 296 | default: |
| 297 | break; |
| 298 | } |
| 299 | |
| 300 | /* save new state */ |
| 301 | iso_state = iso_state_new; |
Kévin Redon | 2a44dc5 | 2018-07-28 17:14:48 +0200 | [diff] [blame^] | 302 | TRACE_INFO("Changed to ISO 7816-3 state %u\n\r", iso_state); |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 303 | } |
| 304 | |
Kévin Redon | 6971996 | 2018-07-28 17:11:21 +0200 | [diff] [blame] | 305 | const struct value_string data_flags[] = { |
| 306 | { |
| 307 | .value = SNIFF_DATA_FLAG_ERROR_INCOMPLETE, |
| 308 | .str = "incomplete", |
| 309 | }, |
| 310 | { |
| 311 | .value = SNIFF_DATA_FLAG_ERROR_MALFORMED, |
| 312 | .str = "malformed", |
| 313 | }, |
| 314 | { |
| 315 | .value = SNIFF_DATA_FLAG_ERROR_CHECKSUM, |
| 316 | .str = "checksum error", |
| 317 | }, |
| 318 | { |
| 319 | .value = 0, |
| 320 | .str = NULL, |
| 321 | }, |
| 322 | }; |
| 323 | |
| 324 | static void print_flags(const struct value_string* flag_meanings, uint32_t nb_flags, uint32_t flags) { |
| 325 | uint32_t i; |
| 326 | for (i = 0; i < nb_flags; i++) { |
| 327 | if (flags & flag_meanings[i].value) { |
| 328 | printf(flag_meanings[i].str); |
| 329 | flags &= ~flag_meanings[i].value; |
| 330 | if (flags) { |
| 331 | printf(", "); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
Kévin Redon | f82f0f6 | 2018-07-08 15:10:23 +0200 | [diff] [blame] | 337 | static void usb_send_data(enum simtrace_msg_type_sniff type, const uint8_t* data, uint16_t length, uint32_t flags) |
| 338 | { |
| 339 | /* Sanity check */ |
| 340 | if (type != SIMTRACE_MSGT_SNIFF_ATR && type != SIMTRACE_MSGT_SNIFF_PPS && type != SIMTRACE_MSGT_SNIFF_TPDU) { |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | /* Show activity on LED */ |
Kévin Redon | da5578b | 2018-07-08 16:32:20 +0200 | [diff] [blame] | 345 | led_blink(LED_GREEN, BLINK_2F_O); |
Kévin Redon | f82f0f6 | 2018-07-08 15:10:23 +0200 | [diff] [blame] | 346 | |
Kévin Redon | f82f0f6 | 2018-07-08 15:10:23 +0200 | [diff] [blame] | 347 | /* Print message */ |
| 348 | switch (type) { |
| 349 | case SIMTRACE_MSGT_SNIFF_ATR: |
| 350 | printf("ATR"); |
| 351 | break; |
| 352 | case SIMTRACE_MSGT_SNIFF_PPS: |
| 353 | printf("PPS"); |
| 354 | break; |
| 355 | case SIMTRACE_MSGT_SNIFF_TPDU: |
| 356 | printf("TPDU"); |
| 357 | break; |
| 358 | default: |
| 359 | printf("???"); |
| 360 | break; |
| 361 | } |
| 362 | if (flags) { |
| 363 | printf(" ("); |
Kévin Redon | 6971996 | 2018-07-28 17:11:21 +0200 | [diff] [blame] | 364 | print_flags(data_flags, ARRAY_SIZE(data_flags), flags); |
Kévin Redon | a95bb1e | 2018-07-08 17:17:49 +0200 | [diff] [blame] | 365 | putchar(')'); |
Kévin Redon | f82f0f6 | 2018-07-08 15:10:23 +0200 | [diff] [blame] | 366 | } |
| 367 | printf(": "); |
| 368 | uint16_t i; |
| 369 | for (i = 0; i < length; i++) { |
| 370 | printf("%02x ", data[i]); |
| 371 | } |
| 372 | printf("\n\r"); |
Kévin Redon | 411428e | 2018-07-08 15:47:38 +0200 | [diff] [blame] | 373 | |
| 374 | /* Send data over USB */ |
| 375 | struct msgb *usb_msg = usb_msg_alloc_hdr(SIMTRACE_USB_EP_CARD_DATAIN, SIMTRACE_MSGC_SNIFF, type); |
| 376 | if (!usb_msg) { |
| 377 | return; |
| 378 | } |
| 379 | struct sniff_data *usb_sniff_data = (struct sniff_data *) msgb_put(usb_msg, sizeof(*usb_sniff_data)); |
| 380 | usb_sniff_data->flags = flags; |
| 381 | usb_sniff_data->length = length; |
| 382 | uint8_t *sniff_data = msgb_put(usb_msg, usb_sniff_data->length); |
| 383 | memcpy(sniff_data, data, length); |
| 384 | usb_msg_upd_len_and_submit(usb_msg); |
| 385 | |
Kévin Redon | f82f0f6 | 2018-07-08 15:10:23 +0200 | [diff] [blame] | 386 | } |
| 387 | |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 388 | /*! Send current ATR over USB |
Kévin Redon | f82f0f6 | 2018-07-08 15:10:23 +0200 | [diff] [blame] | 389 | * @param[in] flags SNIFF_DATA_FLAG_ data flags |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 390 | * @note Also print the ATR to debug console |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 391 | */ |
Kévin Redon | f82f0f6 | 2018-07-08 15:10:23 +0200 | [diff] [blame] | 392 | static void usb_send_atr(uint32_t flags) |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 393 | { |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 394 | /* Check state */ |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 395 | if (ISO7816_S_IN_ATR != iso_state) { |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 396 | TRACE_WARNING("Can't print ATR in ISO 7816-3 state %u\n\r", iso_state); |
| 397 | return; |
| 398 | } |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 399 | if (atr_i >= ARRAY_SIZE(atr)) { |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 400 | TRACE_ERROR("ATR buffer overflow\n\r"); |
| 401 | return; |
| 402 | } |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 403 | |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 404 | /* Send ATR over USB */ |
Kévin Redon | f82f0f6 | 2018-07-08 15:10:23 +0200 | [diff] [blame] | 405 | usb_send_data(SIMTRACE_MSGT_SNIFF_ATR, atr, atr_i, flags); |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /*! Process ATR byte |
| 409 | * @param[in] byte ATR byte to process |
| 410 | */ |
| 411 | static void process_byte_atr(uint8_t byte) |
| 412 | { |
| 413 | static uint8_t atr_hist_len = 0; /* store the number of expected historical bytes */ |
| 414 | static uint8_t y = 0; /* last mask of the upcoming TA, TB, TC, TD interface bytes */ |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame] | 415 | static uint8_t i = 0; /* interface byte subgroup number */ |
Kévin Redon | f66af0c | 2018-07-11 10:27:13 +0200 | [diff] [blame] | 416 | static uint32_t flags = 0; /* error flag */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 417 | |
| 418 | /* sanity check */ |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 419 | if (ISO7816_S_IN_ATR != iso_state) { |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 420 | TRACE_ERROR("Processing ATR data in wrong ISO 7816-3 state %u\n\r", iso_state); |
| 421 | return; |
| 422 | } |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 423 | if (atr_i >= ARRAY_SIZE(atr)) { |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 424 | TRACE_ERROR("ATR data overflow\n\r"); |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 425 | return; |
| 426 | } |
| 427 | |
| 428 | /* save data for use by other functions */ |
| 429 | atr[atr_i++] = byte; |
| 430 | |
| 431 | /* handle ATR byte depending on current state */ |
| 432 | switch (atr_state) { |
| 433 | case ATR_S_WAIT_TS: /* see ISO/IEC 7816-3:2006 section 8.1 */ |
Kévin Redon | f66af0c | 2018-07-11 10:27:13 +0200 | [diff] [blame] | 434 | flags = 0; |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 435 | switch (byte) { |
| 436 | case 0x23: /* direct convention used, but decoded using inverse convention (a parity error should also have occurred) */ |
| 437 | case 0x30: /* inverse convention used, but decoded using direct convention (a parity error should also have occurred) */ |
| 438 | convention_convert = !convention_convert; |
| 439 | case 0x3b: /* direct convention used and correctly decoded */ |
| 440 | case 0x3f: /* inverse convention used and correctly decoded */ |
| 441 | atr_state = ATR_S_WAIT_T0; /* wait for format byte */ |
| 442 | break; |
| 443 | default: |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 444 | TRACE_WARNING("Invalid TS received\n\r"); |
Kévin Redon | c6b9680 | 2018-07-08 17:18:49 +0200 | [diff] [blame] | 445 | led_blink(LED_RED, BLINK_2F_O); /* indicate error to user */ |
| 446 | usb_send_atr(SNIFF_DATA_FLAG_ERROR_MALFORMED); /* send ATR to host software using USB */ |
| 447 | change_state(ISO7816_S_WAIT_ATR); /* reset state */ |
| 448 | break; |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 449 | } |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame] | 450 | i = 0; /* first interface byte sub-group is coming (T0 is kind of TD0) */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 451 | break; |
| 452 | case ATR_S_WAIT_T0: /* see ISO/IEC 7816-3:2006 section 8.2.2 */ |
| 453 | case ATR_S_WAIT_TD: /* see ISO/IEC 7816-3:2006 section 8.2.3 */ |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 454 | if (ATR_S_WAIT_T0 == atr_state) { |
| 455 | atr_hist_len = (byte & 0x0f); /* save the number of historical bytes */ |
| 456 | } else if (ATR_S_WAIT_TD == atr_state) { |
| 457 | t_protocol_support |= (1<<(byte & 0x0f)); /* remember supported protocol to know if TCK will be present */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 458 | } |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 459 | y = (byte & 0xf0); /* remember upcoming interface bytes */ |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame] | 460 | i++; /* next interface byte sub-group is coming */ |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 461 | if (y & 0x10) { |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 462 | atr_state = ATR_S_WAIT_TA; /* wait for interface byte TA */ |
| 463 | break; |
| 464 | } |
| 465 | case ATR_S_WAIT_TA: /* see ISO/IEC 7816-3:2006 section 8.2.3 */ |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 466 | if (y & 0x20) { |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 467 | atr_state = ATR_S_WAIT_TB; /* wait for interface byte TB */ |
| 468 | break; |
| 469 | } |
| 470 | case ATR_S_WAIT_TB: /* see ISO/IEC 7816-3:2006 section 8.2.3 */ |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 471 | if (y & 0x40) { |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 472 | atr_state = ATR_S_WAIT_TC; /* wait for interface byte TC */ |
| 473 | break; |
| 474 | } |
| 475 | case ATR_S_WAIT_TC: /* see ISO/IEC 7816-3:2006 section 8.2.3 */ |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame] | 476 | /* retrieve WI encoded in TC2*/ |
| 477 | if (ATR_S_WAIT_TC==atr_state && 2==i) { |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 478 | if (0 == byte) { |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame] | 479 | update_wt(10, 0); |
| 480 | } else { |
| 481 | update_wt(byte, 0); |
| 482 | } |
| 483 | } |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 484 | if (y & 0x80) { |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 485 | atr_state = ATR_S_WAIT_TD; /* wait for interface byte TD */ |
| 486 | break; |
| 487 | } else if (atr_hist_len) { |
| 488 | atr_state = ATR_S_WAIT_HIST; /* wait for historical bytes */ |
| 489 | break; |
| 490 | } |
| 491 | case ATR_S_WAIT_HIST: /* see ISO/IEC 7816-3:2006 section 8.2.4 */ |
| 492 | if (atr_hist_len) { |
| 493 | atr_hist_len--; |
| 494 | } |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 495 | if (0 == atr_hist_len) { |
| 496 | if (t_protocol_support > 1) { |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 497 | atr_state = ATR_S_WAIT_TCK; /* wait for check bytes */ |
| 498 | break; |
| 499 | } |
| 500 | } else { |
| 501 | break; |
| 502 | } |
| 503 | case ATR_S_WAIT_TCK: /* see ISO/IEC 7816-3:2006 section 8.2.5 */ |
Kévin Redon | f66af0c | 2018-07-11 10:27:13 +0200 | [diff] [blame] | 504 | /* verify checksum if present */ |
| 505 | if (ATR_S_WAIT_TCK == atr_state) { |
| 506 | uint8_t ui; |
| 507 | uint8_t checksum = 0; |
Kévin Redon | ec396bf | 2018-07-28 17:13:41 +0200 | [diff] [blame] | 508 | for (ui = 1; ui < atr_i; ui++) { |
Kévin Redon | f66af0c | 2018-07-11 10:27:13 +0200 | [diff] [blame] | 509 | checksum ^= atr[ui]; |
| 510 | } |
| 511 | if (checksum) { |
| 512 | flags |= SNIFF_DATA_FLAG_ERROR_CHECKSUM; |
| 513 | /* We still consider the data as valid (e.g. for WT) even is the checksum is wrong. |
| 514 | * It is up to the reader to handle this error (e.g. by resetting) |
| 515 | */ |
| 516 | } |
| 517 | } |
| 518 | usb_send_atr(flags); /* send ATR to host software using USB */ |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 519 | change_state(ISO7816_S_WAIT_TPDU); /* go to next state */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 520 | break; |
| 521 | default: |
| 522 | TRACE_INFO("Unknown ATR state %u\n\r", atr_state); |
| 523 | } |
| 524 | } |
| 525 | |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 526 | /*! Send current PPS over USB |
Kévin Redon | f82f0f6 | 2018-07-08 15:10:23 +0200 | [diff] [blame] | 527 | * @param[in] flags SNIFF_DATA_FLAG_ data flags |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 528 | * @note Also print the PPS over the debug console |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 529 | */ |
Kévin Redon | f82f0f6 | 2018-07-08 15:10:23 +0200 | [diff] [blame] | 530 | static void usb_send_pps(uint32_t flags) |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 531 | { |
| 532 | uint8_t *pps_cur; /* current PPS (request or response) */ |
| 533 | |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 534 | /* Sanity check */ |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 535 | if (ISO7816_S_IN_PPS_REQ == iso_state) { |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 536 | pps_cur = pps_req; |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 537 | } else if (ISO7816_S_IN_PPS_RSP == iso_state) { |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 538 | pps_cur = pps_rsp; |
| 539 | } else { |
| 540 | TRACE_ERROR("Can't print PPS in ISO 7816-3 state %u\n\r", iso_state); |
| 541 | return; |
| 542 | } |
| 543 | |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 544 | /* Get only relevant data */ |
| 545 | uint8_t pps[6]; |
| 546 | uint8_t pps_i = 0; |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 547 | if (pps_state > PPS_S_WAIT_PPSS) { |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 548 | pps[pps_i++] = pps_cur[0]; |
| 549 | } |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 550 | if (pps_state > PPS_S_WAIT_PPS0) { |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 551 | pps[pps_i++] = pps_cur[1]; |
| 552 | } |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 553 | if (pps_state > PPS_S_WAIT_PPS1 && pps_cur[1] & 0x10) { |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 554 | pps[pps_i++] = pps_cur[2]; |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 555 | } |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 556 | if (pps_state > PPS_S_WAIT_PPS2 && pps_cur[1] & 0x20) { |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 557 | pps[pps_i++] = pps_cur[3]; |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 558 | } |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 559 | if (pps_state > PPS_S_WAIT_PPS3 && pps_cur[1] & 0x40) { |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 560 | pps[pps_i++] = pps_cur[4]; |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 561 | } |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 562 | if (pps_state > PPS_S_WAIT_PCK) { |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 563 | pps[pps_i++] = pps_cur[5]; |
| 564 | } |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 565 | |
| 566 | /* Send message over USB */ |
Kévin Redon | f82f0f6 | 2018-07-08 15:10:23 +0200 | [diff] [blame] | 567 | usb_send_data(SIMTRACE_MSGT_SNIFF_PPS, pps, pps_i, flags); |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /*! Send Fi/Di change over USB |
| 571 | * @param[in] fidi Fi/Di factor as encoded in TA1 |
| 572 | */ |
| 573 | static void usb_send_fidi(uint8_t fidi) |
| 574 | { |
| 575 | /* Send message over USB */ |
| 576 | struct msgb *usb_msg = usb_msg_alloc_hdr(SIMTRACE_USB_EP_CARD_DATAIN, SIMTRACE_MSGC_SNIFF, SIMTRACE_MSGT_SNIFF_FIDI); |
| 577 | if (!usb_msg) { |
| 578 | return; |
| 579 | } |
| 580 | struct sniff_fidi *usb_sniff_fidi = (struct sniff_fidi *) msgb_put(usb_msg, sizeof(*usb_sniff_fidi)); |
| 581 | usb_sniff_fidi->fidi = fidi; |
| 582 | usb_msg_upd_len_and_submit(usb_msg); |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 583 | } |
| 584 | |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 585 | static void process_byte_pps(uint8_t byte) |
| 586 | { |
| 587 | uint8_t *pps_cur; /* current PPS (request or response) */ |
Kévin Redon | f66af0c | 2018-07-11 10:27:13 +0200 | [diff] [blame] | 588 | static uint32_t flags = 0; /* error flag */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 589 | |
| 590 | /* sanity check */ |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 591 | if (ISO7816_S_IN_PPS_REQ == iso_state) { |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 592 | pps_cur = pps_req; |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 593 | } else if (ISO7816_S_IN_PPS_RSP == iso_state) { |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 594 | pps_cur = pps_rsp; |
| 595 | } else { |
| 596 | TRACE_ERROR("Processing PPS data in wrong ISO 7816-3 state %u\n\r", iso_state); |
| 597 | return; |
| 598 | } |
| 599 | |
| 600 | /* handle PPS byte depending on current state */ |
| 601 | switch (pps_state) { /* see ISO/IEC 7816-3:2006 section 9.2 */ |
| 602 | case PPS_S_WAIT_PPSS: /*!< initial byte */ |
Kévin Redon | f66af0c | 2018-07-11 10:27:13 +0200 | [diff] [blame] | 603 | flags = 0; |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 604 | if (0xff) { |
| 605 | pps_cur[0] = byte; |
| 606 | pps_state = PPS_S_WAIT_PPS0; /* go to next state */ |
| 607 | } else { |
| 608 | TRACE_INFO("Invalid PPSS received\n\r"); |
Kévin Redon | c6b9680 | 2018-07-08 17:18:49 +0200 | [diff] [blame] | 609 | led_blink(LED_RED, BLINK_2F_O); /* indicate error to user */ |
| 610 | usb_send_pps(SNIFF_DATA_FLAG_ERROR_MALFORMED); /* send ATR to host software using USB */ |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 611 | change_state(ISO7816_S_WAIT_TPDU); /* go back to TPDU state */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 612 | } |
| 613 | break; |
| 614 | case PPS_S_WAIT_PPS0: /*!< format byte */ |
| 615 | pps_cur[1] = byte; |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 616 | if (pps_cur[1] & 0x10) { |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 617 | pps_state = PPS_S_WAIT_PPS1; /* go to next state */ |
| 618 | break; |
| 619 | } |
| 620 | case PPS_S_WAIT_PPS1: /*!< first parameter byte */ |
| 621 | pps_cur[2] = byte; /* not always right but doesn't affect the process */ |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 622 | if (pps_cur[1] & 0x20) { |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 623 | pps_state = PPS_S_WAIT_PPS2; /* go to next state */ |
| 624 | break; |
| 625 | } |
| 626 | case PPS_S_WAIT_PPS2: /*!< second parameter byte */ |
| 627 | pps_cur[3] = byte; /* not always right but doesn't affect the process */ |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 628 | if (pps_cur[1] & 0x40) { |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 629 | pps_state = PPS_S_WAIT_PPS3; /* go to next state */ |
| 630 | break; |
| 631 | } |
| 632 | case PPS_S_WAIT_PPS3: /*!< third parameter byte */ |
| 633 | pps_cur[4] = byte; /* not always right but doesn't affect the process */ |
| 634 | pps_state = PPS_S_WAIT_PCK; /* go to next state */ |
| 635 | break; |
| 636 | case PPS_S_WAIT_PCK: /*!< check byte */ |
| 637 | pps_cur[5] = byte; /* not always right but doesn't affect the process */ |
| 638 | /* verify the checksum */ |
| 639 | uint8_t check = 0; |
| 640 | check ^= pps_cur[0]; |
| 641 | check ^= pps_cur[1]; |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 642 | if (pps_cur[1] & 0x10) { |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 643 | check ^= pps_cur[2]; |
| 644 | } |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 645 | if (pps_cur[1] & 0x20) { |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 646 | check ^= pps_cur[3]; |
| 647 | } |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 648 | if (pps_cur[1] & 0x40) { |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 649 | check ^= pps_cur[4]; |
| 650 | } |
| 651 | check ^= pps_cur[5]; |
Kévin Redon | f66af0c | 2018-07-11 10:27:13 +0200 | [diff] [blame] | 652 | if (check) { |
| 653 | flags |= SNIFF_DATA_FLAG_ERROR_CHECKSUM; |
| 654 | } |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 655 | pps_state = PPS_S_WAIT_END; |
Kévin Redon | f66af0c | 2018-07-11 10:27:13 +0200 | [diff] [blame] | 656 | usb_send_pps(flags); /* send PPS to host software using USB */ |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 657 | if (ISO7816_S_IN_PPS_REQ == iso_state) { |
| 658 | if (0 == check) { /* checksum is valid */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 659 | change_state(ISO7816_S_WAIT_PPS_RSP); /* go to next state */ |
| 660 | } else { /* checksum is invalid */ |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 661 | change_state(ISO7816_S_WAIT_TPDU); /* go to next state */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 662 | } |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 663 | } else if (ISO7816_S_IN_PPS_RSP == iso_state) { |
| 664 | if (0 == check) { /* checksum is valid */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 665 | uint8_t fn, dn; |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 666 | if (pps_cur[1] & 0x10) { |
| 667 | fn = (pps_cur[2] >> 4); |
| 668 | dn = (pps_cur[2] & 0x0f); |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 669 | } else { |
| 670 | fn = 1; |
| 671 | dn = 1; |
| 672 | } |
Kévin Redon | 7406337 | 2018-07-03 15:57:03 +0200 | [diff] [blame] | 673 | TRACE_INFO("PPS negotiation successful: Fn=%u Dn=%u\n\r", fi_table[fn], di_table[dn]); |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 674 | update_fidi(&sniff_usart, pps_cur[2]); |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame] | 675 | update_wt(0, di_table[dn]); |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 676 | usb_send_fidi(pps_cur[2]); /* send Fi/Di change notification to host software over USB */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 677 | } else { /* checksum is invalid */ |
| 678 | TRACE_INFO("PPS negotiation failed\n\r"); |
| 679 | } |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 680 | change_state(ISO7816_S_WAIT_TPDU); /* go to next state */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 681 | } |
| 682 | break; |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 683 | case PPS_S_WAIT_END: |
| 684 | TRACE_WARNING("Unexpected PPS received %u\n\r", pps_state); |
| 685 | break; |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 686 | default: |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 687 | TRACE_WARNING("Unknown PPS state %u\n\r", pps_state); |
| 688 | break; |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 692 | /*! Send current TPDU over USB |
Kévin Redon | f82f0f6 | 2018-07-08 15:10:23 +0200 | [diff] [blame] | 693 | * @param[in] flags SNIFF_DATA_FLAG_ data flags |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 694 | * @note Also print the TPDU over the debug console |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 695 | */ |
Kévin Redon | f82f0f6 | 2018-07-08 15:10:23 +0200 | [diff] [blame] | 696 | static void usb_send_tpdu(uint32_t flags) |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 697 | { |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 698 | /* Check state */ |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 699 | if (ISO7816_S_IN_TPDU != iso_state) { |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 700 | TRACE_WARNING("Can't print TPDU in ISO 7816-3 state %u\n\r", iso_state); |
| 701 | return; |
| 702 | } |
| 703 | |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 704 | /* Send ATR over USB */ |
Kévin Redon | f82f0f6 | 2018-07-08 15:10:23 +0200 | [diff] [blame] | 705 | usb_send_data(SIMTRACE_MSGT_SNIFF_TPDU, tpdu_packet, tpdu_packet_i, flags); |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | static void process_byte_tpdu(uint8_t byte) |
| 709 | { |
| 710 | /* sanity check */ |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 711 | if (ISO7816_S_IN_TPDU != iso_state) { |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 712 | TRACE_ERROR("Processing TPDU data in wrong ISO 7816-3 state %u\n\r", iso_state); |
| 713 | return; |
| 714 | } |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 715 | if (tpdu_packet_i >= ARRAY_SIZE(tpdu_packet)) { |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 716 | TRACE_ERROR("TPDU data overflow\n\r"); |
| 717 | return; |
| 718 | } |
| 719 | |
| 720 | /* handle TPDU byte depending on current state */ |
| 721 | switch (tpdu_state) { |
| 722 | case TPDU_S_CLA: |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 723 | if (0xff == byte) { |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 724 | TRACE_WARNING("0xff is not a valid class byte\n\r"); |
Kévin Redon | c6b9680 | 2018-07-08 17:18:49 +0200 | [diff] [blame] | 725 | led_blink(LED_RED, BLINK_2F_O); /* indicate error to user */ |
| 726 | usb_send_tpdu(SNIFF_DATA_FLAG_ERROR_MALFORMED); /* send ATR to host software using USB */ |
Kévin Redon | c9bd715 | 2018-07-03 16:17:00 +0200 | [diff] [blame] | 727 | change_state(ISO7816_S_WAIT_TPDU); /* go back to TPDU state */ |
| 728 | return; |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 729 | } |
| 730 | tpdu_packet_i = 0; |
| 731 | tpdu_packet[tpdu_packet_i++] = byte; |
| 732 | tpdu_state = TPDU_S_INS; |
| 733 | break; |
| 734 | case TPDU_S_INS: |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 735 | if ((0x60 == (byte & 0xf0)) || (0x90 == (byte & 0xf0))) { |
Kévin Redon | c9bd715 | 2018-07-03 16:17:00 +0200 | [diff] [blame] | 736 | TRACE_WARNING("invalid CLA 0x%02x\n\r", byte); |
Kévin Redon | c6b9680 | 2018-07-08 17:18:49 +0200 | [diff] [blame] | 737 | led_blink(LED_RED, BLINK_2F_O); /* indicate error to user */ |
| 738 | usb_send_tpdu(SNIFF_DATA_FLAG_ERROR_MALFORMED); /* send ATR to host software using USB */ |
Kévin Redon | c9bd715 | 2018-07-03 16:17:00 +0200 | [diff] [blame] | 739 | change_state(ISO7816_S_WAIT_TPDU); /* go back to TPDU state */ |
| 740 | return; |
| 741 | } |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 742 | tpdu_packet_i = 1; |
| 743 | tpdu_packet[tpdu_packet_i++] = byte; |
| 744 | tpdu_state = TPDU_S_P1; |
| 745 | break; |
| 746 | case TPDU_S_P1: |
| 747 | tpdu_packet_i = 2; |
| 748 | tpdu_packet[tpdu_packet_i++] = byte; |
| 749 | tpdu_state = TPDU_S_P2; |
| 750 | break; |
| 751 | case TPDU_S_P2: |
| 752 | tpdu_packet_i = 3; |
| 753 | tpdu_packet[tpdu_packet_i++] = byte; |
| 754 | tpdu_state = TPDU_S_P3; |
| 755 | break; |
| 756 | case TPDU_S_P3: |
| 757 | tpdu_packet_i = 4; |
| 758 | tpdu_packet[tpdu_packet_i++] = byte; |
| 759 | tpdu_state = TPDU_S_PROCEDURE; |
| 760 | break; |
| 761 | case TPDU_S_PROCEDURE: |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 762 | if (0x60 == byte) { /* wait for next procedure byte */ |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 763 | break; |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 764 | } else if (tpdu_packet[1] == byte) { /* get all remaining data bytes */ |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 765 | tpdu_state = TPDU_S_DATA_REMAINING; |
| 766 | break; |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 767 | } else if ((~tpdu_packet[1]) == byte) { /* get single data byte */ |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 768 | tpdu_state = TPDU_S_DATA_SINGLE; |
| 769 | break; |
| 770 | } |
| 771 | case TPDU_S_SW1: |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 772 | if ((0x60 == (byte & 0xf0)) || (0x90 == (byte & 0xf0))) { /* this procedure byte is SW1 */ |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 773 | tpdu_packet[tpdu_packet_i++] = byte; |
| 774 | tpdu_state = TPDU_S_SW2; |
| 775 | } else { |
| 776 | TRACE_WARNING("invalid SW1 0x%02x\n\r", byte); |
Kévin Redon | c6b9680 | 2018-07-08 17:18:49 +0200 | [diff] [blame] | 777 | led_blink(LED_RED, BLINK_2F_O); /* indicate error to user */ |
| 778 | usb_send_tpdu(SNIFF_DATA_FLAG_ERROR_MALFORMED); /* send ATR to host software using USB */ |
Kévin Redon | c9bd715 | 2018-07-03 16:17:00 +0200 | [diff] [blame] | 779 | change_state(ISO7816_S_WAIT_TPDU); /* go back to TPDU state */ |
| 780 | return; |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 781 | } |
| 782 | break; |
| 783 | case TPDU_S_SW2: |
| 784 | tpdu_packet[tpdu_packet_i++] = byte; |
Kévin Redon | f82f0f6 | 2018-07-08 15:10:23 +0200 | [diff] [blame] | 785 | usb_send_tpdu(0); /* send TPDU to host software using USB */ |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 786 | change_state(ISO7816_S_WAIT_TPDU); /* this is the end of the TPDU */ |
| 787 | break; |
| 788 | case TPDU_S_DATA_SINGLE: |
| 789 | case TPDU_S_DATA_REMAINING: |
| 790 | tpdu_packet[tpdu_packet_i++] = byte; |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 791 | if (0 == tpdu_packet[4]) { |
| 792 | if (5+256 <= tpdu_packet_i) { |
Kévin Redon | c9bd715 | 2018-07-03 16:17:00 +0200 | [diff] [blame] | 793 | tpdu_state = TPDU_S_PROCEDURE; |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 794 | } |
| 795 | } else { |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 796 | if (5+tpdu_packet[4] <= tpdu_packet_i) { |
Kévin Redon | c9bd715 | 2018-07-03 16:17:00 +0200 | [diff] [blame] | 797 | tpdu_state = TPDU_S_PROCEDURE; |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 798 | } |
| 799 | } |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 800 | if (TPDU_S_DATA_SINGLE == tpdu_state) { |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 801 | tpdu_state = TPDU_S_PROCEDURE; |
| 802 | } |
| 803 | break; |
| 804 | default: |
| 805 | TRACE_ERROR("unhandled TPDU state %u\n\r", tpdu_state); |
| 806 | } |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 807 | } |
| 808 | |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 809 | /*! Interrupt Service Routine called on USART activity */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 810 | void Sniffer_usart_isr(void) |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 811 | { |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame] | 812 | /* Remaining Waiting Time (WI) counter (>16 bits) */ |
| 813 | static volatile uint32_t wt_remaining = 9600; |
| 814 | |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 815 | /* Read channel status register */ |
Kévin Redon | 709a431 | 2018-07-03 16:10:04 +0200 | [diff] [blame] | 816 | uint32_t csr = sniff_usart.base->US_CSR; |
| 817 | /* Verify if there was an error */ |
| 818 | if (csr & US_CSR_OVRE) { |
| 819 | TRACE_WARNING("USART overrun error\n\r"); |
| 820 | sniff_usart.base->US_CR |= US_CR_RSTSTA; |
| 821 | } |
| 822 | if (csr & US_CSR_FRAME) { |
| 823 | TRACE_WARNING("USART framing error\n\r"); |
| 824 | sniff_usart.base->US_CR |= US_CR_RSTSTA; |
| 825 | } |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame] | 826 | |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 827 | /* Verify if character has been received */ |
| 828 | if (csr & US_CSR_RXRDY) { |
| 829 | /* Read communication data byte between phone and SIM */ |
| 830 | uint8_t byte = sniff_usart.base->US_RHR; |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame] | 831 | /* Reset WT timer */ |
| 832 | wt_remaining = wt; |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 833 | /* Store sniffed data into buffer (also clear interrupt */ |
Kévin Redon | 709a431 | 2018-07-03 16:10:04 +0200 | [diff] [blame] | 834 | if (rbuf_is_full(&sniff_buffer)) { |
| 835 | TRACE_ERROR("USART buffer full\n\r"); |
| 836 | } else { |
| 837 | rbuf_write(&sniff_buffer, byte); |
| 838 | } |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 839 | } |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame] | 840 | |
Kévin Redon | 638cec8 | 2018-06-27 16:42:56 +0200 | [diff] [blame] | 841 | /* Verify it WT timeout occurred, to detect unresponsive card */ |
| 842 | if (csr & US_CSR_TIMEOUT) { |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 843 | if (wt_remaining <= (sniff_usart.base->US_RTOR & 0xffff)) { |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame] | 844 | /* Just set the flag and let the main loop handle it */ |
| 845 | change_flags |= SNIFF_CHANGE_FLAG_TIMEOUT_WT; |
| 846 | /* Reset timeout value */ |
| 847 | wt_remaining = wt; |
| 848 | } else { |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 849 | wt_remaining -= (sniff_usart.base->US_RTOR & 0xffff); /* be sure to subtract the actual timeout since the new might not have been set and reloaded yet */ |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame] | 850 | } |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 851 | if (wt_remaining > 0xffff) { |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame] | 852 | sniff_usart.base->US_RTOR = 0xffff; |
| 853 | } else { |
| 854 | sniff_usart.base->US_RTOR = wt_remaining; |
| 855 | } |
| 856 | /* Stop timeout until next character is received (and clears the timeout flag) */ |
Kévin Redon | 638cec8 | 2018-06-27 16:42:56 +0200 | [diff] [blame] | 857 | sniff_usart.base->US_CR |= US_CR_STTTO; |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame] | 858 | if (!(change_flags & SNIFF_CHANGE_FLAG_TIMEOUT_WT)) { |
| 859 | /* Immediately restart the counter it the WT timeout did not occur (needs the timeout flag to be cleared) */ |
| 860 | sniff_usart.base->US_CR |= US_CR_RETTO; |
| 861 | } |
Kévin Redon | 638cec8 | 2018-06-27 16:42:56 +0200 | [diff] [blame] | 862 | } |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 863 | } |
| 864 | |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 865 | /** PIO interrupt service routine to checks if the card reset line has changed |
| 866 | */ |
| 867 | static void Sniffer_reset_isr(const Pin* pPin) |
| 868 | { |
| 869 | /* Ensure an edge on the reset pin cause the interrupt */ |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 870 | if (pPin->id != pin_rst.id || 0 == (pPin->mask & pin_rst.mask)) { |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 871 | TRACE_ERROR("Pin other than reset caused a interrupt\n\r"); |
| 872 | return; |
| 873 | } |
Kévin Redon | f000831 | 2018-07-10 15:58:40 +0200 | [diff] [blame] | 874 | /* Update the ISO state according to the reset change (reset is active low) */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 875 | if (PIO_Get(&pin_rst)) { |
Kévin Redon | 8e84f81 | 2018-07-26 15:34:03 +0200 | [diff] [blame] | 876 | change_flags |= SNIFF_CHANGE_FLAG_RESET_DEASSERT; /* set flag and let main loop send it */ |
Kévin Redon | f000831 | 2018-07-10 15:58:40 +0200 | [diff] [blame] | 877 | } else { |
Kévin Redon | 8e84f81 | 2018-07-26 15:34:03 +0200 | [diff] [blame] | 878 | change_flags |= SNIFF_CHANGE_FLAG_RESET_ASSERT; /* set flag and let main loop send it */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 882 | /*------------------------------------------------------------------------------ |
| 883 | * Global functions |
| 884 | *------------------------------------------------------------------------------*/ |
| 885 | |
| 886 | void Sniffer_usart1_irq(void) |
| 887 | { |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 888 | if (ID_USART1 == sniff_usart.id) { |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 889 | Sniffer_usart_isr(); |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 890 | } |
| 891 | } |
| 892 | |
| 893 | void Sniffer_usart0_irq(void) |
| 894 | { |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 895 | if (ID_USART0 == sniff_usart.id) { |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 896 | Sniffer_usart_isr(); |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 900 | /*----------------------------------------------------------------------------- |
Christina Quast | d2b05f0 | 2015-02-25 18:44:24 +0100 | [diff] [blame] | 901 | * Initialization routine |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 902 | *-----------------------------------------------------------------------------*/ |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 903 | |
Harald Welte | ed75c62 | 2017-11-28 21:23:12 +0100 | [diff] [blame] | 904 | /* Called during USB enumeration after device is enumerated by host */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 905 | void Sniffer_configure(void) |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 906 | { |
Kévin Redon | 36abece | 2018-06-04 16:30:01 +0200 | [diff] [blame] | 907 | TRACE_INFO("Sniffer config\n\r"); |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 908 | } |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 909 | |
Harald Welte | ed75c62 | 2017-11-28 21:23:12 +0100 | [diff] [blame] | 910 | /* called when *different* configuration is set by host */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 911 | void Sniffer_exit(void) |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 912 | { |
Kévin Redon | 36abece | 2018-06-04 16:30:01 +0200 | [diff] [blame] | 913 | TRACE_INFO("Sniffer exit\n\r"); |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 914 | /* Disable USART */ |
Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 915 | USART_DisableIt(sniff_usart.base, US_IER_RXRDY); |
| 916 | /* NOTE: don't forget to set the IRQ according to the USART peripheral used */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 917 | NVIC_DisableIRQ(IRQ_USART_SIM); |
Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 918 | USART_SetReceiverEnabled(sniff_usart.base, 0); |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 919 | /* Disable RST IRQ */ |
| 920 | PIO_DisableIt(&pin_rst); |
| 921 | NVIC_DisableIRQ(PIOA_IRQn); /* CAUTION this needs to match to the correct port */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 922 | } |
| 923 | |
Kévin Redon | 36abece | 2018-06-04 16:30:01 +0200 | [diff] [blame] | 924 | /* called when *Sniffer* configuration is set by host */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 925 | void Sniffer_init(void) |
| 926 | { |
Kévin Redon | 36abece | 2018-06-04 16:30:01 +0200 | [diff] [blame] | 927 | TRACE_INFO("Sniffer Init\n\r"); |
Kévin Redon | 7b73462 | 2018-06-06 16:13:48 +0200 | [diff] [blame] | 928 | |
Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 929 | /* Configure pins to sniff communication between phone and card */ |
| 930 | PIO_Configure(pins_sniff, PIO_LISTSIZE(pins_sniff)); |
| 931 | /* Configure pins to connect phone to card */ |
| 932 | PIO_Configure(pins_bus, PIO_LISTSIZE(pins_bus)); |
| 933 | /* Configure pins to forward phone power to card */ |
| 934 | PIO_Configure(pins_power, PIO_LISTSIZE(pins_power)); |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 935 | /* Enable interrupts on port with reset line */ |
| 936 | NVIC_EnableIRQ(PIOA_IRQn); /* CAUTION this needs to match to the correct port */ |
| 937 | /* Register ISR to handle card reset change */ |
| 938 | PIO_ConfigureIt(&pin_rst, &Sniffer_reset_isr); |
| 939 | /* Enable interrupt on card reset pin */ |
| 940 | PIO_EnableIt(&pin_rst); |
Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 941 | |
Kévin Redon | 7b73462 | 2018-06-06 16:13:48 +0200 | [diff] [blame] | 942 | /* Clear ring buffer containing the sniffed data */ |
| 943 | rbuf_reset(&sniff_buffer); |
Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 944 | /* Configure USART to as ISO-7816 slave communication to sniff communication */ |
| 945 | ISO7816_Init(&sniff_usart, CLK_SLAVE); |
| 946 | /* Only receive data when sniffing */ |
| 947 | USART_SetReceiverEnabled(sniff_usart.base, 1); |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame] | 948 | /* Enable Receiver time-out to detect waiting time (WT) time-out (e.g. unresponsive cards) */ |
| 949 | sniff_usart.base->US_RTOR = wt; |
Kévin Redon | 638cec8 | 2018-06-27 16:42:56 +0200 | [diff] [blame] | 950 | /* Enable interrupt to indicate when data has been received or timeout occurred */ |
| 951 | USART_EnableIt(sniff_usart.base, US_IER_RXRDY | US_IER_TIMEOUT); |
Kévin Redon | fe763b7 | 2018-07-03 16:17:56 +0200 | [diff] [blame] | 952 | /* Set USB priority lower than USART to not miss sniffing data (both at 0 per default) */ |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 953 | if (NVIC_GetPriority(IRQ_USART_SIM) >= NVIC_GetPriority(UDP_IRQn)) { |
| 954 | NVIC_SetPriority(UDP_IRQn, NVIC_GetPriority(IRQ_USART_SIM) + 2); |
Kévin Redon | fe763b7 | 2018-07-03 16:17:56 +0200 | [diff] [blame] | 955 | } |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 956 | /* Enable interrupt requests for the USART peripheral */ |
| 957 | NVIC_EnableIRQ(IRQ_USART_SIM); |
| 958 | |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 959 | /* Reset state */ |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 960 | if (ISO7816_S_RESET != iso_state) { |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 961 | change_state(ISO7816_S_RESET); |
| 962 | } |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 963 | } |
| 964 | |
Kévin Redon | 709a431 | 2018-07-03 16:10:04 +0200 | [diff] [blame] | 965 | /*! Send card change flags over USB |
| 966 | * @param[in] flags change flags corresponding to SIMTRACE_MSGT_SNIFF_CHANGE |
| 967 | */ |
| 968 | static void usb_send_change(uint32_t flags) |
| 969 | { |
| 970 | /* Check flags */ |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 971 | if(0 == flags) { /* no changes */ |
Kévin Redon | 709a431 | 2018-07-03 16:10:04 +0200 | [diff] [blame] | 972 | return; |
| 973 | } |
| 974 | |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 975 | if (flags & SNIFF_CHANGE_FLAG_TIMEOUT_WT) { |
Kévin Redon | 709a431 | 2018-07-03 16:10:04 +0200 | [diff] [blame] | 976 | printf("waiting time (WT) timeout\n\r"); |
| 977 | } |
| 978 | |
| 979 | /* Send message over USB */ |
| 980 | struct msgb *usb_msg = usb_msg_alloc_hdr(SIMTRACE_USB_EP_CARD_DATAIN, SIMTRACE_MSGC_SNIFF, SIMTRACE_MSGT_SNIFF_CHANGE); |
| 981 | if (!usb_msg) { |
| 982 | return; |
| 983 | } |
| 984 | struct sniff_change *usb_sniff_change = (struct sniff_change *) msgb_put(usb_msg, sizeof(*usb_sniff_change)); |
| 985 | usb_sniff_change->flags = flags; |
| 986 | usb_msg_upd_len_and_submit(usb_msg); |
| 987 | } |
| 988 | |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 989 | /* Main (idle/busy) loop of this USB configuration */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 990 | void Sniffer_run(void) |
| 991 | { |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 992 | /* Handle USB queue */ |
| 993 | /* first try to send any pending messages on INT */ |
| 994 | usb_refill_to_host(SIMTRACE_USB_EP_CARD_INT); |
| 995 | /* then try to send any pending messages on IN */ |
| 996 | usb_refill_to_host(SIMTRACE_USB_EP_CARD_DATAIN); |
| 997 | /* ensure we can handle incoming USB messages from the host */ |
| 998 | /* currently we don't need any incoming data |
| 999 | usb_refill_from_host(SIMTRACE_USB_EP_CARD_DATAOUT); |
| 1000 | struct llist_head *queue = usb_get_queue(SIMTRACE_USB_EP_CARD_DATAOUT); |
| 1001 | process_any_usb_commands(queue); |
| 1002 | */ |
| 1003 | |
Kévin Redon | a2fccba | 2018-07-08 15:49:46 +0200 | [diff] [blame] | 1004 | /* WARNING: the signal data and flags are not synchronized. We have to hope |
| 1005 | * the processing is fast enough to not land in the wrong state while data |
| 1006 | * is remaining |
| 1007 | */ |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 1008 | /* Handle sniffed data */ |
| 1009 | if (!rbuf_is_empty(&sniff_buffer)) { /* use if instead of while to let the main loop restart the watchdog */ |
| 1010 | uint8_t byte = rbuf_read(&sniff_buffer); |
Kévin Redon | 709a431 | 2018-07-03 16:10:04 +0200 | [diff] [blame] | 1011 | /* Convert convention if required */ |
| 1012 | if (convention_convert) { |
| 1013 | byte = convention_convert_lut[byte]; |
| 1014 | } |
| 1015 | //TRACE_ERROR_WP(">%02x", byte); |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 1016 | switch (iso_state) { /* Handle byte depending on state */ |
| 1017 | case ISO7816_S_RESET: /* During reset we shouldn't receive any data */ |
| 1018 | break; |
| 1019 | case ISO7816_S_WAIT_ATR: /* After a reset we expect the ATR */ |
| 1020 | change_state(ISO7816_S_IN_ATR); /* go to next state */ |
| 1021 | case ISO7816_S_IN_ATR: /* More ATR data incoming */ |
| 1022 | process_byte_atr(byte); |
| 1023 | break; |
| 1024 | case ISO7816_S_WAIT_TPDU: /* After the ATR we expect TPDU or PPS data */ |
| 1025 | case ISO7816_S_WAIT_PPS_RSP: |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 1026 | if (0xff == byte) { |
| 1027 | if (ISO7816_S_WAIT_PPS_RSP == iso_state) { |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 1028 | change_state(ISO7816_S_IN_PPS_RSP); /* Go to PPS state */ |
| 1029 | } else { |
| 1030 | change_state(ISO7816_S_IN_PPS_REQ); /* Go to PPS state */ |
| 1031 | } |
| 1032 | process_byte_pps(byte); |
| 1033 | break; |
| 1034 | } |
| 1035 | case ISO7816_S_IN_TPDU: /* More TPDU data incoming */ |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 1036 | if (ISO7816_S_WAIT_TPDU == iso_state) { |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 1037 | change_state(ISO7816_S_IN_TPDU); |
| 1038 | } |
| 1039 | process_byte_tpdu(byte); |
| 1040 | break; |
| 1041 | case ISO7816_S_IN_PPS_REQ: |
| 1042 | case ISO7816_S_IN_PPS_RSP: |
| 1043 | process_byte_pps(byte); |
| 1044 | break; |
| 1045 | default: |
| 1046 | TRACE_ERROR("Data received in unknown state %u\n\r", iso_state); |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | /* Handle flags */ |
| 1051 | if (change_flags) { /* WARNING this is not synced with the data buffer handling */ |
Kévin Redon | 8e84f81 | 2018-07-26 15:34:03 +0200 | [diff] [blame] | 1052 | if (change_flags & SNIFF_CHANGE_FLAG_RESET_ASSERT) { |
Kévin Redon | f000831 | 2018-07-10 15:58:40 +0200 | [diff] [blame] | 1053 | if (ISO7816_S_RESET != iso_state) { |
| 1054 | change_state(ISO7816_S_RESET); |
Kévin Redon | 2a44dc5 | 2018-07-28 17:14:48 +0200 | [diff] [blame^] | 1055 | printf("reset asserted\n\r"); |
Kévin Redon | a2fccba | 2018-07-08 15:49:46 +0200 | [diff] [blame] | 1056 | } |
| 1057 | } |
Kévin Redon | 8e84f81 | 2018-07-26 15:34:03 +0200 | [diff] [blame] | 1058 | if (change_flags & SNIFF_CHANGE_FLAG_RESET_DEASSERT) { |
Kévin Redon | f000831 | 2018-07-10 15:58:40 +0200 | [diff] [blame] | 1059 | if (ISO7816_S_WAIT_ATR != iso_state) { |
| 1060 | change_state(ISO7816_S_WAIT_ATR); |
Kévin Redon | 2a44dc5 | 2018-07-28 17:14:48 +0200 | [diff] [blame^] | 1061 | printf("reset de-asserted\n\r"); |
Kévin Redon | a2fccba | 2018-07-08 15:49:46 +0200 | [diff] [blame] | 1062 | } |
| 1063 | } |
Kévin Redon | 55f0612 | 2018-07-08 14:13:29 +0200 | [diff] [blame] | 1064 | if (change_flags & SNIFF_CHANGE_FLAG_TIMEOUT_WT) { |
Kévin Redon | 709a431 | 2018-07-03 16:10:04 +0200 | [diff] [blame] | 1065 | /* Use timeout to detect interrupted data transmission */ |
| 1066 | switch (iso_state) { |
| 1067 | case ISO7816_S_IN_ATR: |
Kévin Redon | c6b9680 | 2018-07-08 17:18:49 +0200 | [diff] [blame] | 1068 | led_blink(LED_RED, BLINK_2F_O); /* indicate error to user */ |
Kévin Redon | f82f0f6 | 2018-07-08 15:10:23 +0200 | [diff] [blame] | 1069 | usb_send_atr(SNIFF_DATA_FLAG_ERROR_INCOMPLETE); /* send incomplete ATR to host software using USB */ |
Kévin Redon | 709a431 | 2018-07-03 16:10:04 +0200 | [diff] [blame] | 1070 | change_state(ISO7816_S_WAIT_ATR); |
| 1071 | break; |
| 1072 | case ISO7816_S_IN_TPDU: |
Kévin Redon | c6b9680 | 2018-07-08 17:18:49 +0200 | [diff] [blame] | 1073 | led_blink(LED_RED, BLINK_2F_O); /* indicate error to user */ |
Kévin Redon | f82f0f6 | 2018-07-08 15:10:23 +0200 | [diff] [blame] | 1074 | usb_send_tpdu(SNIFF_DATA_FLAG_ERROR_INCOMPLETE); /* send incomplete PPS to host software using USB */ |
Kévin Redon | 709a431 | 2018-07-03 16:10:04 +0200 | [diff] [blame] | 1075 | change_state(ISO7816_S_WAIT_TPDU); |
| 1076 | break; |
| 1077 | case ISO7816_S_IN_PPS_REQ: |
| 1078 | case ISO7816_S_IN_PPS_RSP: |
Kévin Redon | c6b9680 | 2018-07-08 17:18:49 +0200 | [diff] [blame] | 1079 | led_blink(LED_RED, BLINK_2F_O); /* indicate error to user */ |
Kévin Redon | f82f0f6 | 2018-07-08 15:10:23 +0200 | [diff] [blame] | 1080 | usb_send_pps(SNIFF_DATA_FLAG_ERROR_INCOMPLETE); /* send incomplete TPDU to host software using USB */ |
Kévin Redon | 709a431 | 2018-07-03 16:10:04 +0200 | [diff] [blame] | 1081 | change_state(ISO7816_S_WAIT_TPDU); |
| 1082 | break; |
| 1083 | default: |
| 1084 | change_flags &= ~SNIFF_CHANGE_FLAG_TIMEOUT_WT; /* We don't care about the timeout is all other cases */ |
| 1085 | break; |
| 1086 | } |
| 1087 | } |
| 1088 | if (change_flags) { |
| 1089 | usb_send_change(change_flags); /* send timeout to host software over USB */ |
| 1090 | change_flags = 0; /* Reset flags */ |
| 1091 | } |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 1092 | } |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 1093 | } |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 1094 | #endif /* HAVE_SNIFFER */ |