blob: 27f102ded9607374eda502c00a91194a7e717cf1 [file] [log] [blame]
Kévin Redon45ad62d2018-06-07 18:56:41 +02001/*
2 * (C) 2010-2017 by Harald Welte <hwelte@sysmocom.de>
3 * (C) 2018 by Kevin Redon <kredon@sysmocom.de>
4 * All Rights Reserved
Christina Quasta90eefa2015-02-24 17:52:29 +01005 *
Kévin Redon45ad62d2018-06-07 18:56:41 +02006 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
Christina Quasta90eefa2015-02-24 17:52:29 +010010 *
Kévin Redon45ad62d2018-06-07 18:56:41 +020011 * 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
14 * GNU Affero General Public License for more details.
Christina Quasta90eefa2015-02-24 17:52:29 +010015 *
Kévin Redon45ad62d2018-06-07 18:56:41 +020016 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Christina Quasta90eefa2015-02-24 17:52:29 +010018 *
Christina Quasta90eefa2015-02-24 17:52:29 +010019 */
Kévin Redon8fa6ff52018-06-25 16:00:33 +020020/* This code implement the Sniffer mode to sniff the communication between a SIM card (or any ISO 7816 smart card) and a phone (or any ISO 7816 card reader).
Kévin Redond7a6de52018-06-11 13:46:35 +020021 * For historical reasons (i.e. SIMtrace hardware) the USART peripheral connected to the SIM card is used.
Kévin Redon8fa6ff52018-06-25 16:00:33 +020022 * TODO handle RST, PTS, and send data over USB
23 * TODO put common ISO7816-3 code is separate library (and combine clean with iso7816_4)
Kévin Redond7a6de52018-06-11 13:46:35 +020024 */
Kévin Redon36abece2018-06-04 16:30:01 +020025#include "board.h"
26#include "simtrace.h"
27
Harald Welte2fb59962016-02-28 12:34:26 +010028#ifdef HAVE_SNIFFER
29
Christina Quasta90eefa2015-02-24 17:52:29 +010030/*------------------------------------------------------------------------------
31 * Headers
32 *------------------------------------------------------------------------------*/
33
Christina Quasta90eefa2015-02-24 17:52:29 +010034#include <string.h>
Kévin Redon8fa6ff52018-06-25 16:00:33 +020035#include "utils.h"
36#include "iso7816_fidi.h"
Kévin Redonde97fd22018-07-01 18:23:23 +020037/* USB related libraries */
38#include "osmocom/core/linuxlist.h"
39#include "osmocom/core/msgb.h"
40#include "llist_irqsafe.h"
41#include "usb_buf.h"
42#include "simtrace_usb.h"
43#include "simtrace_prot.h"
Christina Quasta90eefa2015-02-24 17:52:29 +010044
45/*------------------------------------------------------------------------------
46 * Internal definitions
47 *------------------------------------------------------------------------------*/
48
Kévin Redon8fa6ff52018-06-25 16:00:33 +020049/*! Maximum Answer-To-Reset (ATR) size in bytes
Kévin Redond7a6de52018-06-11 13:46:35 +020050 * @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
51 * @remark technical there is no size limitation since Yi present in T0,TDi will indicate if more interface bytes are present, including TDi+i
52 */
53#define MAX_ATR_SIZE 33
Kévin Redon8fa6ff52018-06-25 16:00:33 +020054/*! Maximum Protocol and Parameters Selection (PPS) size in bytes
55 * @note defined in ISO/IEC 7816-3:2006(E) section 9.2
56 */
57#define MAX_PPS_SIZE 6
Kévin Redond7a6de52018-06-11 13:46:35 +020058
59/*! ISO 7816-3 states relevant to the sniff mode */
60enum iso7816_3_sniff_state {
61 ISO7816_S_RESET, /*!< in Reset */
62 ISO7816_S_WAIT_ATR, /*!< waiting for ATR to start */
63 ISO7816_S_IN_ATR, /*!< while we are receiving the ATR */
Kévin Redon00ec89d2018-06-27 16:41:52 +020064 ISO7816_S_WAIT_TPDU, /*!< waiting for start of new TPDU */
65 ISO7816_S_IN_TPDU, /*!< inside a single TPDU */
Kévin Redon8fa6ff52018-06-25 16:00:33 +020066 ISO7816_S_IN_PPS_REQ, /*!< while we are inside the PPS request */
67 ISO7816_S_WAIT_PPS_RSP, /*!< waiting for start of the PPS response */
68 ISO7816_S_IN_PPS_RSP, /*!< while we are inside the PPS request */
Kévin Redond7a6de52018-06-11 13:46:35 +020069};
70
71/*! Answer-To-Reset (ATR) sub-states of ISO7816_S_IN_ATR
72 * @note defined in ISO/IEC 7816-3:2006(E) section 8
73 */
74enum atr_sniff_state {
75 ATR_S_WAIT_TS, /*!< initial byte */
76 ATR_S_WAIT_T0, /*!< format byte */
77 ATR_S_WAIT_TA, /*!< first sub-group interface byte */
78 ATR_S_WAIT_TB, /*!< second sub-group interface byte */
79 ATR_S_WAIT_TC, /*!< third sub-group interface byte */
80 ATR_S_WAIT_TD, /*!< fourth sub-group interface byte */
81 ATR_S_WAIT_HIST, /*!< historical byte */
82 ATR_S_WAIT_TCK, /*!< check byte */
Kévin Redon8fa6ff52018-06-25 16:00:33 +020083};
84
85/*! Protocol and Parameters Selection (PPS) sub-states of ISO7816_S_IN_PTS_REQ/ISO7816_S_IN_PTS_RSP
86 * @note defined in ISO/IEC 7816-3:2006(E) section 9
87 */
88enum pps_sniff_state {
89 PPS_S_WAIT_PPSS, /*!< initial byte */
90 PPS_S_WAIT_PPS0, /*!< format byte */
91 PPS_S_WAIT_PPS1, /*!< first parameter byte */
92 PPS_S_WAIT_PPS2, /*!< second parameter byte */
93 PPS_S_WAIT_PPS3, /*!< third parameter byte */
94 PPS_S_WAIT_PCK, /*!< check byte */
Kévin Redond7a6de52018-06-11 13:46:35 +020095};
Kévin Redon45ad62d2018-06-07 18:56:41 +020096
Kévin Redon00ec89d2018-06-27 16:41:52 +020097/*! Transport Protocol Data Unit (TPDU) sub-states of ISO7816_S_IN_TPDU
98 * @note defined in ISO/IEC 7816-3:2006(E) section 10 and 12
99 * @remark APDUs are formed by one or more command+response TPDUs
100 */
101enum tpdu_sniff_state {
102 TPDU_S_CLA, /*!< class byte */
103 TPDU_S_INS, /*!< instruction byte */
104 TPDU_S_P1, /*!< first parameter byte for the instruction */
105 TPDU_S_P2, /*!< second parameter byte for the instruction */
106 TPDU_S_P3, /*!< third parameter byte encoding the data length */
107 TPDU_S_PROCEDURE, /*!< procedure byte (could also be SW1) */
108 TPDU_S_DATA_REMAINING, /*!< remaining data bytes */
109 TPDU_S_DATA_SINGLE, /*!< single data byte */
110 TPDU_S_SW1, /*!< first status word */
111 TPDU_S_SW2, /*!< second status word */
112};
113
Christina Quasta90eefa2015-02-24 17:52:29 +0100114/*------------------------------------------------------------------------------
115 * Internal variables
116 *------------------------------------------------------------------------------*/
Kévin Redond7a6de52018-06-11 13:46:35 +0200117
118/* note: the sniffer code is currently designed to support only one sniffing interface, but the hardware would support a second one.
119 * to support a second sniffer interface the code should be restructured to use handles.
120 */
121/* Pin configurations */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200122/*! Pin configuration to sniff communication (using USART connection card) */
Kévin Redonee62a9d2018-06-11 13:42:23 +0200123static const Pin pins_sniff[] = { PINS_SIM_SNIFF };
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200124/*! Pin configuration to interconnect phone and card using the bus switch */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100125static const Pin pins_bus[] = { PINS_BUS_SNIFF };
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200126/*! Pin configuration to power the card by the phone */
Kévin Redond7a6de52018-06-11 13:46:35 +0200127static const Pin pins_power[] = { PINS_PWR_SNIFF };
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200128/*! Pin configuration for timer counter to measure ETU timing */
Kévin Redon45ad62d2018-06-07 18:56:41 +0200129static const Pin pins_tc[] = { PINS_TC };
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200130/*! Pin configuration for card reset line */
131static const Pin pin_rst = PIN_SIM_RST_SNIFF;
132
Kévin Redond7a6de52018-06-11 13:46:35 +0200133/* USART related variables */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200134/*! USART peripheral used to sniff communication */
Kévin Redon45ad62d2018-06-07 18:56:41 +0200135static struct Usart_info sniff_usart = {
136 .base = USART_SIM,
137 .id = ID_USART_SIM,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100138 .state = USART_RCV,
139};
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200140/*! Ring buffer to store sniffer communication data */
Kévin Redon7b734622018-06-06 16:13:48 +0200141static struct ringbuf sniff_buffer;
142
Kévin Redonde97fd22018-07-01 18:23:23 +0200143/* Flags to know is the card status changed (see SIMTRACE_MSGT_DT_SNIFF_CHANGE flags) */
144volatile uint32_t change_flags = 0;
145
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200146/* ISO 7816 variables */
147/*! ISO 7816-3 state */
148enum iso7816_3_sniff_state iso_state = ISO7816_S_RESET;
149/*! ATR state */
150enum atr_sniff_state atr_state;
151/*! ATR data
152 * @remark can be used to check later protocol changes
153 */
154uint8_t atr[MAX_ATR_SIZE];
155/*! Current index in the ATR data */
156uint8_t atr_i = 0;
157/*! If convention conversion is needed */
158bool convention_convert = false;
159/*! The supported T protocols */
160uint16_t t_protocol_support = 0;
161/*! PPS state
162 * @remark it is shared between request and response since they aren't simultaneous but follow the same procedure
163 */
164enum pps_sniff_state pps_state;
165/*! PPS request data
166 * @remark can be used to check PPS response
167 */
168uint8_t pps_req[MAX_PPS_SIZE];
169/*! PPS response data */
170uint8_t pps_rsp[MAX_PPS_SIZE];
Kévin Redon00ec89d2018-06-27 16:41:52 +0200171/*! TPDU state */
172enum tpdu_sniff_state tpdu_state;
173/*! Final TPDU packet
174 * @note this is the complete command+response TPDU, including header, data, and status words
175 * @remark this does not include the procedure bytes
176 */
177uint8_t tpdu_packet[5+256+2];
178/*! Current index in TPDU packet */
179uint8_t tpdu_packet_i = 0;
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200180
Kévin Redon7b734622018-06-06 16:13:48 +0200181/*------------------------------------------------------------------------------
Kévin Redon7b734622018-06-06 16:13:48 +0200182 * Internal functions
183 *------------------------------------------------------------------------------*/
Kévin Redon36abece2018-06-04 16:30:01 +0200184
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200185/*! Convert data between direct and inverse convention
186 * @note direct convention is LSb first and HIGH=1; inverse conversion in MSb first and LOW=1
187 * @remark use a look up table to speed up conversion
188 */
189static 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, };
190
Kévin Redonde97fd22018-07-01 18:23:23 +0200191/*! Allocate USB buffer and push + initialize simtrace_msg_hdr
192 * @param[in] ep USB IN endpoint where the message will be sent to
193 * @param[in] msg_class SIMtrace USB message class
194 * @param[in] msg_type SIMtrace USB message type
195 * @return USB message with allocated ans initialized header, or NULL if allocation failed
196 */
197static struct msgb *usb_msg_alloc_hdr(uint8_t ep, uint8_t msg_class, uint8_t msg_type)
198{
199 struct msgb *usb_msg = usb_buf_alloc(SIMTRACE_USB_EP_CARD_DATAIN);
200 if (!usb_msg) {
201 return NULL;
202 }
203 struct simtrace_msg_hdr *usb_msg_header;
204 usb_msg->l1h = msgb_put(usb_msg, sizeof(*usb_msg_header));
205 usb_msg_header = (struct simtrace_msg_hdr *) usb_msg->l1h;
206 memset(usb_msg_header, 0, sizeof(*usb_msg_header));
207 usb_msg_header->msg_class = SIMTRACE_MSGC_SNIFF;
208 usb_msg_header->msg_type = SIMTRACE_MSGT_SNIFF_CHANGE;
209 usb_msg->l2h = usb_msg->l1h + sizeof(*usb_msg_header);
210
211 return usb_msg;
212}
213
214/* update SIMtrace header msg_len and submit USB buffer
215 * param[in] usb_msg USB message to update and send
216 */
217void usb_msg_upd_len_and_submit(struct msgb *usb_msg)
218{
219 struct simtrace_msg_hdr *usb_msg_header = (struct simtrace_msg_hdr *) usb_msg->l1h;
220 usb_msg_header->msg_len = msgb_length(usb_msg);
221 usb_buf_submit(usb_msg);
222}
223
224/*! Send card change flags over USB
225 * @param[in] flags change flags corresponding to SIMTRACE_MSGT_SNIFF_CHANGE
226 */
227static void usb_send_change(uint32_t flags)
228{
229 /* Check flags */
230 if(0==flags) { /* no changes */
231 return;
232 }
233
234 /* Send message over USB */
235 struct msgb *usb_msg = usb_msg_alloc_hdr(SIMTRACE_USB_EP_CARD_DATAIN, SIMTRACE_MSGC_SNIFF, SIMTRACE_MSGT_SNIFF_CHANGE);
236 if (!usb_msg) {
237 return;
238 }
239 struct sniff_change *usb_sniff_change = (struct sniff_change *) msgb_put(usb_msg, sizeof(*usb_sniff_change));
240 usb_sniff_change->flags = flags;
241 usb_msg_upd_len_and_submit(usb_msg);
242}
243
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200244/*! Update the ISO 7816-3 state
245 * @param[in] iso_state_new new ISO 7816-3 state to update to
246 */
247static void change_state(enum iso7816_3_sniff_state iso_state_new)
248{
249 /* sanity check */
250 if (iso_state_new==iso_state) {
251 TRACE_WARNING("Already in ISO 7816 state %u\n\r", iso_state);
252 return;
253 }
254
255 /* handle actions to perform when switching state */
256 switch (iso_state_new) {
257 case ISO7816_S_RESET:
258 update_fidi(&sniff_usart, 0x11); /* reset baud rate to default Di/Fi values */
Kévin Redonde97fd22018-07-01 18:23:23 +0200259 usb_send_change(SNIFF_CHANGE_FLAG_RESET_HOLD); /* send reset change to host software over USB */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200260 break;
261 case ISO7816_S_WAIT_ATR:
262 rbuf_reset(&sniff_buffer); /* reset buffer for new communication */
Kévin Redonde97fd22018-07-01 18:23:23 +0200263 usb_send_change(SNIFF_CHANGE_FLAG_RESET_RELEASE); /* send reset change to host software over USB */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200264 break;
265 case ISO7816_S_IN_ATR:
266 atr_i = 0;
267 convention_convert = false;
268 t_protocol_support = 0;
269 atr_state = ATR_S_WAIT_TS;
270 break;
271 case ISO7816_S_IN_PPS_REQ:
272 case ISO7816_S_IN_PPS_RSP:
273 pps_state = PPS_S_WAIT_PPSS;
274 break;
Kévin Redon00ec89d2018-06-27 16:41:52 +0200275 case ISO7816_S_WAIT_TPDU:
276 tpdu_state = TPDU_S_CLA;
277 tpdu_packet_i = 0;
278 break;
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200279 default:
280 break;
281 }
282
283 /* save new state */
284 iso_state = iso_state_new;
Kévin Redoncf599192018-06-27 16:38:31 +0200285 //TRACE_INFO("Changed to ISO 7816-3 state %u\n\r", iso_state); /* don't print since this is function is also called by ISRs */
286}
287
Kévin Redonde97fd22018-07-01 18:23:23 +0200288/*! Send current ATR over USB
289 * @note Also print the ATR over serial
290 */
291static void usb_send_atr(void)
Kévin Redoncf599192018-06-27 16:38:31 +0200292{
Kévin Redonde97fd22018-07-01 18:23:23 +0200293 /* Check state */
Kévin Redoncf599192018-06-27 16:38:31 +0200294 if (ISO7816_S_IN_ATR!=iso_state) {
295 TRACE_WARNING("Can't print ATR in ISO 7816-3 state %u\n\r", iso_state);
296 return;
297 }
Kévin Redonde97fd22018-07-01 18:23:23 +0200298 if (atr_i>=ARRAY_SIZE(atr)) {
299 TRACE_ERROR("ATR buffer overflow\n\r");
300 return;
301 }
Kévin Redoncf599192018-06-27 16:38:31 +0200302
Kévin Redonde97fd22018-07-01 18:23:23 +0200303 /* Show activity on LED */
Kévin Redoncf599192018-06-27 16:38:31 +0200304 led_blink(LED_GREEN, BLINK_2O_F);
Kévin Redonde97fd22018-07-01 18:23:23 +0200305
306 /* Print ATR */
Kévin Redoncf599192018-06-27 16:38:31 +0200307 printf("ATR: ");
308 uint8_t i;
Kévin Redonde97fd22018-07-01 18:23:23 +0200309 for (i = 0; i < atr_i; i++) {
Kévin Redoncf599192018-06-27 16:38:31 +0200310 printf("%02x ", atr[i]);
311 }
312 printf("\n\r");
Kévin Redonde97fd22018-07-01 18:23:23 +0200313
314 /* Send ATR over USB */
315 struct msgb *usb_msg = usb_msg_alloc_hdr(SIMTRACE_USB_EP_CARD_DATAIN, SIMTRACE_MSGC_SNIFF, SIMTRACE_MSGT_SNIFF_ATR);
316 if (!usb_msg) {
317 return;
318 }
319 struct sniff_data *usb_sniff_data_atr = (struct sniff_data *) msgb_put(usb_msg, sizeof(*usb_sniff_data_atr));
320 usb_sniff_data_atr->complete = true;
321 usb_sniff_data_atr->length = atr_i;
322 uint8_t *data = msgb_put(usb_msg, usb_sniff_data_atr->length);
323 memcpy(data, atr, atr_i);
324 usb_msg_upd_len_and_submit(usb_msg);
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200325}
326
327/*! Process ATR byte
328 * @param[in] byte ATR byte to process
329 */
330static void process_byte_atr(uint8_t byte)
331{
332 static uint8_t atr_hist_len = 0; /* store the number of expected historical bytes */
333 static uint8_t y = 0; /* last mask of the upcoming TA, TB, TC, TD interface bytes */
334
335 /* sanity check */
336 if (ISO7816_S_IN_ATR!=iso_state) {
337 TRACE_ERROR("Processing ATR data in wrong ISO 7816-3 state %u\n\r", iso_state);
338 return;
339 }
340 if (atr_i>=ARRAY_SIZE(atr)) {
Kévin Redoncf599192018-06-27 16:38:31 +0200341 TRACE_ERROR("ATR data overflow\n\r");
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200342 return;
343 }
344
345 /* save data for use by other functions */
346 atr[atr_i++] = byte;
347
348 /* handle ATR byte depending on current state */
349 switch (atr_state) {
350 case ATR_S_WAIT_TS: /* see ISO/IEC 7816-3:2006 section 8.1 */
351 switch (byte) {
352 case 0x23: /* direct convention used, but decoded using inverse convention (a parity error should also have occurred) */
353 case 0x30: /* inverse convention used, but decoded using direct convention (a parity error should also have occurred) */
354 convention_convert = !convention_convert;
355 case 0x3b: /* direct convention used and correctly decoded */
356 case 0x3f: /* inverse convention used and correctly decoded */
357 atr_state = ATR_S_WAIT_T0; /* wait for format byte */
358 break;
359 default:
360 atr_i--; /* revert last byte */
361 TRACE_WARNING("Invalid TS received\n\r");
362 }
363 break;
364 case ATR_S_WAIT_T0: /* see ISO/IEC 7816-3:2006 section 8.2.2 */
365 case ATR_S_WAIT_TD: /* see ISO/IEC 7816-3:2006 section 8.2.3 */
366 if (ATR_S_WAIT_T0==atr_state) {
367 atr_hist_len = (byte&0x0f); /* save the number of historical bytes */
368 } else if (ATR_S_WAIT_TD==atr_state) {
369 t_protocol_support |= (1<<(byte&0x0f)); /* remember supported protocol to know if TCK will be present */
370 }
371 y = (byte&0xf0); /* remember upcoming interface bytes */
372 if (y&0x10) {
373 atr_state = ATR_S_WAIT_TA; /* wait for interface byte TA */
374 break;
375 }
376 case ATR_S_WAIT_TA: /* see ISO/IEC 7816-3:2006 section 8.2.3 */
377 if (y&0x20) {
378 atr_state = ATR_S_WAIT_TB; /* wait for interface byte TB */
379 break;
380 }
381 case ATR_S_WAIT_TB: /* see ISO/IEC 7816-3:2006 section 8.2.3 */
382 if (y&0x40) {
383 atr_state = ATR_S_WAIT_TC; /* wait for interface byte TC */
384 break;
385 }
386 case ATR_S_WAIT_TC: /* see ISO/IEC 7816-3:2006 section 8.2.3 */
387 if (y&0x80) {
388 atr_state = ATR_S_WAIT_TD; /* wait for interface byte TD */
389 break;
390 } else if (atr_hist_len) {
391 atr_state = ATR_S_WAIT_HIST; /* wait for historical bytes */
392 break;
393 }
394 case ATR_S_WAIT_HIST: /* see ISO/IEC 7816-3:2006 section 8.2.4 */
395 if (atr_hist_len) {
396 atr_hist_len--;
397 }
398 if (0==atr_hist_len) {
399 if (t_protocol_support>1) {
400 atr_state = ATR_S_WAIT_TCK; /* wait for check bytes */
401 break;
402 }
403 } else {
404 break;
405 }
406 case ATR_S_WAIT_TCK: /* see ISO/IEC 7816-3:2006 section 8.2.5 */
407 /* we could verify the checksum, but we are just here to sniff */
Kévin Redonde97fd22018-07-01 18:23:23 +0200408 usb_send_atr(); /* send ATR to host software using USB */
Kévin Redon00ec89d2018-06-27 16:41:52 +0200409 change_state(ISO7816_S_WAIT_TPDU); /* go to next state */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200410 break;
411 default:
412 TRACE_INFO("Unknown ATR state %u\n\r", atr_state);
413 }
414}
415
Kévin Redonde97fd22018-07-01 18:23:23 +0200416/*! Send current PPS over USB
417 * @note Also print the PPS over serial
418 */
419static void usb_send_pps(void)
Kévin Redoncf599192018-06-27 16:38:31 +0200420{
421 uint8_t *pps_cur; /* current PPS (request or response) */
422
Kévin Redonde97fd22018-07-01 18:23:23 +0200423 /* Sanity check */
Kévin Redoncf599192018-06-27 16:38:31 +0200424 if (ISO7816_S_IN_PPS_REQ==iso_state) {
425 pps_cur = pps_req;
426 } else if (ISO7816_S_IN_PPS_RSP==iso_state) {
427 pps_cur = pps_rsp;
428 } else {
429 TRACE_ERROR("Can't print PPS in ISO 7816-3 state %u\n\r", iso_state);
430 return;
431 }
432
Kévin Redonde97fd22018-07-01 18:23:23 +0200433 /* Get only relevant data */
434 uint8_t pps[6];
435 uint8_t pps_i = 0;
436 pps[pps_i++] = pps_cur[0];
437 pps[pps_i++] = pps_cur[1];
Kévin Redoncf599192018-06-27 16:38:31 +0200438 if (pps_cur[1]&0x10) {
Kévin Redonde97fd22018-07-01 18:23:23 +0200439 pps[pps_i++] = pps_cur[2];
Kévin Redoncf599192018-06-27 16:38:31 +0200440 }
441 if (pps_cur[1]&0x20) {
Kévin Redonde97fd22018-07-01 18:23:23 +0200442 pps[pps_i++] = pps_cur[3];
Kévin Redoncf599192018-06-27 16:38:31 +0200443 }
444 if (pps_cur[1]&0x40) {
Kévin Redonde97fd22018-07-01 18:23:23 +0200445 pps[pps_i++] = pps_cur[4];
Kévin Redoncf599192018-06-27 16:38:31 +0200446 }
Kévin Redonde97fd22018-07-01 18:23:23 +0200447 pps[pps_i++] = pps_cur[5];
448
449 /* Show activity on LED */
450 led_blink(LED_GREEN, BLINK_2O_F);
451
452 /* Print PPS */
453 printf("PPS: ");
454 uint8_t i;
455 for (i = 0; i < pps_i; i++) {
456 printf("%02x ", pps[i]);
457 }
Kévin Redoncf599192018-06-27 16:38:31 +0200458 printf("\n\r");
Kévin Redonde97fd22018-07-01 18:23:23 +0200459
460 /* Send message over USB */
461 struct msgb *usb_msg = usb_msg_alloc_hdr(SIMTRACE_USB_EP_CARD_DATAIN, SIMTRACE_MSGC_SNIFF, SIMTRACE_MSGT_SNIFF_PPS);
462 if (!usb_msg) {
463 return;
464 }
465 struct sniff_data *usb_sniff_data_pps = (struct sniff_data *) msgb_put(usb_msg, sizeof(*usb_sniff_data_pps));
466 usb_sniff_data_pps->complete = true;
467 usb_sniff_data_pps->length = pps_i;
468 uint8_t *data = msgb_put(usb_msg, usb_sniff_data_pps->length);
469 memcpy(data, pps, pps_i);
470 usb_msg_upd_len_and_submit(usb_msg);
471}
472
473/*! Send Fi/Di change over USB
474 * @param[in] fidi Fi/Di factor as encoded in TA1
475 */
476static void usb_send_fidi(uint8_t fidi)
477{
478 /* Send message over USB */
479 struct msgb *usb_msg = usb_msg_alloc_hdr(SIMTRACE_USB_EP_CARD_DATAIN, SIMTRACE_MSGC_SNIFF, SIMTRACE_MSGT_SNIFF_FIDI);
480 if (!usb_msg) {
481 return;
482 }
483 struct sniff_fidi *usb_sniff_fidi = (struct sniff_fidi *) msgb_put(usb_msg, sizeof(*usb_sniff_fidi));
484 usb_sniff_fidi->fidi = fidi;
485 usb_msg_upd_len_and_submit(usb_msg);
Kévin Redoncf599192018-06-27 16:38:31 +0200486}
487
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200488static void process_byte_pps(uint8_t byte)
489{
490 uint8_t *pps_cur; /* current PPS (request or response) */
491
492 /* sanity check */
493 if (ISO7816_S_IN_PPS_REQ==iso_state) {
494 pps_cur = pps_req;
495 } else if (ISO7816_S_IN_PPS_RSP==iso_state) {
496 pps_cur = pps_rsp;
497 } else {
498 TRACE_ERROR("Processing PPS data in wrong ISO 7816-3 state %u\n\r", iso_state);
499 return;
500 }
501
502 /* handle PPS byte depending on current state */
503 switch (pps_state) { /* see ISO/IEC 7816-3:2006 section 9.2 */
504 case PPS_S_WAIT_PPSS: /*!< initial byte */
505 if (0xff) {
506 pps_cur[0] = byte;
507 pps_state = PPS_S_WAIT_PPS0; /* go to next state */
508 } else {
509 TRACE_INFO("Invalid PPSS received\n\r");
Kévin Redon00ec89d2018-06-27 16:41:52 +0200510 change_state(ISO7816_S_WAIT_TPDU); /* go back to TPDU state */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200511 }
512 break;
513 case PPS_S_WAIT_PPS0: /*!< format byte */
514 pps_cur[1] = byte;
515 if (pps_cur[1]&0x10) {
516 pps_state = PPS_S_WAIT_PPS1; /* go to next state */
517 break;
518 }
519 case PPS_S_WAIT_PPS1: /*!< first parameter byte */
520 pps_cur[2] = byte; /* not always right but doesn't affect the process */
521 if (pps_cur[1]&0x20) {
522 pps_state = PPS_S_WAIT_PPS2; /* go to next state */
523 break;
524 }
525 case PPS_S_WAIT_PPS2: /*!< second parameter byte */
526 pps_cur[3] = byte; /* not always right but doesn't affect the process */
527 if (pps_cur[1]&0x40) {
528 pps_state = PPS_S_WAIT_PPS3; /* go to next state */
529 break;
530 }
531 case PPS_S_WAIT_PPS3: /*!< third parameter byte */
532 pps_cur[4] = byte; /* not always right but doesn't affect the process */
533 pps_state = PPS_S_WAIT_PCK; /* go to next state */
534 break;
535 case PPS_S_WAIT_PCK: /*!< check byte */
536 pps_cur[5] = byte; /* not always right but doesn't affect the process */
537 /* verify the checksum */
538 uint8_t check = 0;
539 check ^= pps_cur[0];
540 check ^= pps_cur[1];
541 if (pps_cur[1]&0x10) {
542 check ^= pps_cur[2];
543 }
544 if (pps_cur[1]&0x20) {
545 check ^= pps_cur[3];
546 }
547 if (pps_cur[1]&0x40) {
548 check ^= pps_cur[4];
549 }
550 check ^= pps_cur[5];
Kévin Redonde97fd22018-07-01 18:23:23 +0200551 usb_send_pps(); /* send PPS to host software using USB */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200552 if (ISO7816_S_IN_PPS_REQ==iso_state) {
553 if (0==check) { /* checksum is valid */
554 change_state(ISO7816_S_WAIT_PPS_RSP); /* go to next state */
555 } else { /* checksum is invalid */
Kévin Redon00ec89d2018-06-27 16:41:52 +0200556 change_state(ISO7816_S_WAIT_TPDU); /* go to next state */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200557 }
558 } else if (ISO7816_S_IN_PPS_RSP==iso_state) {
559 if (0==check) { /* checksum is valid */
560 uint8_t fn, dn;
561 if (pps_cur[1]&0x10) {
562 fn = (pps_cur[2]>>4);
563 dn = (pps_cur[2]&0x0f);
564 } else {
565 fn = 1;
566 dn = 1;
567 }
568 TRACE_INFO("PPS negotiation successful: Fn=%u Dn=%u\n\r", fn, dn);
569 update_fidi(&sniff_usart, pps_cur[2]);
Kévin Redonde97fd22018-07-01 18:23:23 +0200570 usb_send_fidi(pps_cur[2]); /* send Fi/Di change notification to host software over USB */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200571 } else { /* checksum is invalid */
572 TRACE_INFO("PPS negotiation failed\n\r");
573 }
Kévin Redon00ec89d2018-06-27 16:41:52 +0200574 change_state(ISO7816_S_WAIT_TPDU); /* go to next state */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200575 }
576 break;
577 default:
578 TRACE_INFO("Unknown PPS state %u\n\r", pps_state);
579 }
580}
581
Kévin Redonde97fd22018-07-01 18:23:23 +0200582/*! Send current TPDU over USB
583 * @note Also print the TPDU over serial
584 */
585static void usb_send_tpdu(void)
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200586{
Kévin Redonde97fd22018-07-01 18:23:23 +0200587 /* Check state */
Kévin Redon00ec89d2018-06-27 16:41:52 +0200588 if (ISO7816_S_IN_TPDU!=iso_state) {
589 TRACE_WARNING("Can't print TPDU in ISO 7816-3 state %u\n\r", iso_state);
590 return;
591 }
592
Kévin Redonde97fd22018-07-01 18:23:23 +0200593 /* Show activity on LED */
Kévin Redon00ec89d2018-06-27 16:41:52 +0200594 led_blink(LED_GREEN, BLINK_2O_F);
Kévin Redonde97fd22018-07-01 18:23:23 +0200595
596 /* Print TPDU */
Kévin Redon00ec89d2018-06-27 16:41:52 +0200597 printf("TPDU: ");
598 uint16_t i;
599 for (i = 0; i < tpdu_packet_i && i < ARRAY_SIZE(tpdu_packet); i++) {
600 printf("%02x ", tpdu_packet[i]);
601 }
602 printf("\n\r");
Kévin Redonde97fd22018-07-01 18:23:23 +0200603
604 /* Send ATR over USB */
605 struct msgb *usb_msg = usb_msg_alloc_hdr(SIMTRACE_USB_EP_CARD_DATAIN, SIMTRACE_MSGC_SNIFF, SIMTRACE_MSGT_SNIFF_TPDU);
606 if (!usb_msg) {
607 return;
608 }
609 struct sniff_data *usb_sniff_data_tpdu = (struct sniff_data *) msgb_put(usb_msg, sizeof(*usb_sniff_data_tpdu));
610 usb_sniff_data_tpdu->complete = true;
611 usb_sniff_data_tpdu->length = tpdu_packet_i;
612 uint8_t *data = msgb_put(usb_msg, usb_sniff_data_tpdu->length);
613 memcpy(data, tpdu_packet, tpdu_packet_i);
614 usb_msg_upd_len_and_submit(usb_msg);
Kévin Redon00ec89d2018-06-27 16:41:52 +0200615}
616
617static void process_byte_tpdu(uint8_t byte)
618{
619 /* sanity check */
620 if (ISO7816_S_IN_TPDU!=iso_state) {
621 TRACE_ERROR("Processing TPDU data in wrong ISO 7816-3 state %u\n\r", iso_state);
622 return;
623 }
624 if (tpdu_packet_i>=ARRAY_SIZE(tpdu_packet)) {
625 TRACE_ERROR("TPDU data overflow\n\r");
626 return;
627 }
628
629 /* handle TPDU byte depending on current state */
630 switch (tpdu_state) {
631 case TPDU_S_CLA:
632 if (0xff==byte) {
633 TRACE_WARNING("0xff is not a valid class byte\n\r");
634 break;
635 }
636 tpdu_packet_i = 0;
637 tpdu_packet[tpdu_packet_i++] = byte;
638 tpdu_state = TPDU_S_INS;
639 break;
640 case TPDU_S_INS:
641 tpdu_packet_i = 1;
642 tpdu_packet[tpdu_packet_i++] = byte;
643 tpdu_state = TPDU_S_P1;
644 break;
645 case TPDU_S_P1:
646 tpdu_packet_i = 2;
647 tpdu_packet[tpdu_packet_i++] = byte;
648 tpdu_state = TPDU_S_P2;
649 break;
650 case TPDU_S_P2:
651 tpdu_packet_i = 3;
652 tpdu_packet[tpdu_packet_i++] = byte;
653 tpdu_state = TPDU_S_P3;
654 break;
655 case TPDU_S_P3:
656 tpdu_packet_i = 4;
657 tpdu_packet[tpdu_packet_i++] = byte;
658 tpdu_state = TPDU_S_PROCEDURE;
659 break;
660 case TPDU_S_PROCEDURE:
661 if (0x60==byte) { /* wait for next procedure byte */
662 break;
663 } else if (tpdu_packet[1]==byte) { /* get all remaining data bytes */
664 tpdu_state = TPDU_S_DATA_REMAINING;
665 break;
666 } else if ((~tpdu_packet[1])==byte) { /* get single data byte */
667 tpdu_state = TPDU_S_DATA_SINGLE;
668 break;
669 }
670 case TPDU_S_SW1:
671 if ((0x60==(byte&0xf0)) || (0x90==(byte&0xf0))) { /* this procedure byte is SW1 */
672 tpdu_packet[tpdu_packet_i++] = byte;
673 tpdu_state = TPDU_S_SW2;
674 } else {
675 TRACE_WARNING("invalid SW1 0x%02x\n\r", byte);
676 }
677 break;
678 case TPDU_S_SW2:
679 tpdu_packet[tpdu_packet_i++] = byte;
Kévin Redonde97fd22018-07-01 18:23:23 +0200680 usb_send_tpdu(); /* send TPDU to host software using USB */
Kévin Redon00ec89d2018-06-27 16:41:52 +0200681 change_state(ISO7816_S_WAIT_TPDU); /* this is the end of the TPDU */
682 break;
683 case TPDU_S_DATA_SINGLE:
684 case TPDU_S_DATA_REMAINING:
685 tpdu_packet[tpdu_packet_i++] = byte;
686 if (0==tpdu_packet[4]) {
687 if (5+256<=tpdu_packet_i) {
688 tpdu_state = TPDU_S_SW1;
689 }
690 } else {
691 if (5+tpdu_packet[4]<=tpdu_packet_i) {
692 tpdu_state = TPDU_S_SW1;
693 }
694 }
695 if (TPDU_S_DATA_SINGLE==tpdu_state) {
696 tpdu_state = TPDU_S_PROCEDURE;
697 }
698 break;
699 default:
700 TRACE_ERROR("unhandled TPDU state %u\n\r", tpdu_state);
701 }
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200702}
703
Kévin Redond7a6de52018-06-11 13:46:35 +0200704/*! Interrupt Service Routine called on USART activity */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200705void Sniffer_usart_isr(void)
Kévin Redond7a6de52018-06-11 13:46:35 +0200706{
707 /* Read channel status register */
708 uint32_t csr = sniff_usart.base->US_CSR & sniff_usart.base->US_IMR;
709 /* Verify if character has been received */
710 if (csr & US_CSR_RXRDY) {
711 /* Read communication data byte between phone and SIM */
712 uint8_t byte = sniff_usart.base->US_RHR;
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200713 /* Convert convention if required */
714 if (convention_convert) {
715 byte = convention_convert_lut[byte];
716 }
Kévin Redond7a6de52018-06-11 13:46:35 +0200717 /* Store sniffed data into buffer (also clear interrupt */
718 rbuf_write(&sniff_buffer, byte);
719 }
Kévin Redon638cec82018-06-27 16:42:56 +0200720 /* Verify it WT timeout occurred, to detect unresponsive card */
721 if (csr & US_CSR_TIMEOUT) {
722 /* Stop timeout until next character is received */
723 sniff_usart.base->US_CR |= US_CR_STTTO;
724 /* Use timeout to detect end of ATR/PPS/TPDU */
725 switch (iso_state) {
726 case ISO7816_S_RESET:
727 case ISO7816_S_WAIT_ATR:
728 break;
729 case ISO7816_S_IN_ATR:
730 change_state(ISO7816_S_WAIT_ATR);
731 break;
732 case ISO7816_S_WAIT_TPDU:
733 break;
734 case ISO7816_S_WAIT_PPS_RSP:
735 case ISO7816_S_IN_TPDU:
736 case ISO7816_S_IN_PPS_REQ:
737 case ISO7816_S_IN_PPS_RSP:
738 change_state(ISO7816_S_WAIT_TPDU);
739 break;
740 default:
741 break;
742 }
Kévin Redonde97fd22018-07-01 18:23:23 +0200743 usb_send_change(SNIFF_CHANGE_FLAG_TIMEOUT_WT); /* send timeout to host software over USB */
Kévin Redon638cec82018-06-27 16:42:56 +0200744 }
Kévin Redond7a6de52018-06-11 13:46:35 +0200745}
746
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200747/** PIO interrupt service routine to checks if the card reset line has changed
748 */
749static void Sniffer_reset_isr(const Pin* pPin)
750{
751 /* Ensure an edge on the reset pin cause the interrupt */
752 if (pPin->id!=pin_rst.id || 0==(pPin->mask&pin_rst.mask)) {
753 TRACE_ERROR("Pin other than reset caused a interrupt\n\r");
754 return;
755 }
756 /* Update the ISO state according to the reset change */
757 if (PIO_Get(&pin_rst)) {
758 if (ISO7816_S_WAIT_ATR!=iso_state) {
759 change_state(ISO7816_S_WAIT_ATR);
760 }
761 } else {
762 if (ISO7816_S_RESET!=iso_state) {
763 change_state(ISO7816_S_RESET);
764 }
765 }
766}
767
Kévin Redond7a6de52018-06-11 13:46:35 +0200768/*------------------------------------------------------------------------------
769 * Global functions
770 *------------------------------------------------------------------------------*/
771
772void Sniffer_usart1_irq(void)
773{
774 if (ID_USART1==sniff_usart.id) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200775 Sniffer_usart_isr();
Kévin Redond7a6de52018-06-11 13:46:35 +0200776 }
777}
778
779void Sniffer_usart0_irq(void)
780{
781 if (ID_USART0==sniff_usart.id) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200782 Sniffer_usart_isr();
Kévin Redond7a6de52018-06-11 13:46:35 +0200783 }
784}
785
Christina Quasta90eefa2015-02-24 17:52:29 +0100786/*-----------------------------------------------------------------------------
Christina Quastd2b05f02015-02-25 18:44:24 +0100787 * Initialization routine
Christina Quasta90eefa2015-02-24 17:52:29 +0100788 *-----------------------------------------------------------------------------*/
Christina Quasta90eefa2015-02-24 17:52:29 +0100789
Harald Welteed75c622017-11-28 21:23:12 +0100790/* Called during USB enumeration after device is enumerated by host */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100791void Sniffer_configure(void)
Christina Quasta90eefa2015-02-24 17:52:29 +0100792{
Kévin Redon36abece2018-06-04 16:30:01 +0200793 TRACE_INFO("Sniffer config\n\r");
Christina Quasta90eefa2015-02-24 17:52:29 +0100794}
Christina Quastfb524b92015-02-27 13:39:45 +0100795
Harald Welteed75c622017-11-28 21:23:12 +0100796/* called when *different* configuration is set by host */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100797void Sniffer_exit(void)
Christina Quastfb524b92015-02-27 13:39:45 +0100798{
Kévin Redon36abece2018-06-04 16:30:01 +0200799 TRACE_INFO("Sniffer exit\n\r");
Kévin Redonde97fd22018-07-01 18:23:23 +0200800 /* Disable USART */
Kévin Redon45ad62d2018-06-07 18:56:41 +0200801 USART_DisableIt(sniff_usart.base, US_IER_RXRDY);
802 /* NOTE: don't forget to set the IRQ according to the USART peripheral used */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200803 NVIC_DisableIRQ(IRQ_USART_SIM);
Kévin Redon45ad62d2018-06-07 18:56:41 +0200804 USART_SetReceiverEnabled(sniff_usart.base, 0);
Kévin Redonde97fd22018-07-01 18:23:23 +0200805 /* Disable RST IRQ */
806 PIO_DisableIt(&pin_rst);
807 NVIC_DisableIRQ(PIOA_IRQn); /* CAUTION this needs to match to the correct port */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100808}
809
Kévin Redon36abece2018-06-04 16:30:01 +0200810/* called when *Sniffer* configuration is set by host */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100811void Sniffer_init(void)
812{
Kévin Redon36abece2018-06-04 16:30:01 +0200813 TRACE_INFO("Sniffer Init\n\r");
Kévin Redon7b734622018-06-06 16:13:48 +0200814
Kévin Redon45ad62d2018-06-07 18:56:41 +0200815 /* Configure pins to sniff communication between phone and card */
816 PIO_Configure(pins_sniff, PIO_LISTSIZE(pins_sniff));
817 /* Configure pins to connect phone to card */
818 PIO_Configure(pins_bus, PIO_LISTSIZE(pins_bus));
819 /* Configure pins to forward phone power to card */
820 PIO_Configure(pins_power, PIO_LISTSIZE(pins_power));
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200821 /* Enable interrupts on port with reset line */
822 NVIC_EnableIRQ(PIOA_IRQn); /* CAUTION this needs to match to the correct port */
823 /* Register ISR to handle card reset change */
824 PIO_ConfigureIt(&pin_rst, &Sniffer_reset_isr);
825 /* Enable interrupt on card reset pin */
826 PIO_EnableIt(&pin_rst);
Kévin Redon45ad62d2018-06-07 18:56:41 +0200827
Kévin Redon7b734622018-06-06 16:13:48 +0200828 /* Clear ring buffer containing the sniffed data */
829 rbuf_reset(&sniff_buffer);
Kévin Redon45ad62d2018-06-07 18:56:41 +0200830 /* Configure USART to as ISO-7816 slave communication to sniff communication */
831 ISO7816_Init(&sniff_usart, CLK_SLAVE);
832 /* Only receive data when sniffing */
833 USART_SetReceiverEnabled(sniff_usart.base, 1);
Kévin Redon638cec82018-06-27 16:42:56 +0200834 /* Enable Receiver time-out WT to detect unresponsive cards */
835 sniff_usart.base->US_RTOR = 9600-12; /* -12 because the timer starts at the end of the 12 ETU frame */
836 /* Enable interrupt to indicate when data has been received or timeout occurred */
837 USART_EnableIt(sniff_usart.base, US_IER_RXRDY | US_IER_TIMEOUT);
Kévin Redond7a6de52018-06-11 13:46:35 +0200838 /* Enable interrupt requests for the USART peripheral */
839 NVIC_EnableIRQ(IRQ_USART_SIM);
840
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200841 /* Reset state */
842 if (ISO7816_S_RESET!=iso_state) {
843 change_state(ISO7816_S_RESET);
844 }
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100845}
846
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200847/* Main (idle/busy) loop of this USB configuration */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100848void Sniffer_run(void)
849{
Kévin Redonde97fd22018-07-01 18:23:23 +0200850 /* Handle USB queue */
851 /* first try to send any pending messages on INT */
852 usb_refill_to_host(SIMTRACE_USB_EP_CARD_INT);
853 /* then try to send any pending messages on IN */
854 usb_refill_to_host(SIMTRACE_USB_EP_CARD_DATAIN);
855 /* ensure we can handle incoming USB messages from the host */
856 /* currently we don't need any incoming data
857 usb_refill_from_host(SIMTRACE_USB_EP_CARD_DATAOUT);
858 struct llist_head *queue = usb_get_queue(SIMTRACE_USB_EP_CARD_DATAOUT);
859 process_any_usb_commands(queue);
860 */
861
862 /* Handle sniffed data */
863 if (!rbuf_is_empty(&sniff_buffer)) { /* use if instead of while to let the main loop restart the watchdog */
864 uint8_t byte = rbuf_read(&sniff_buffer);
865 TRACE_DEBUG_WP("< 0x%02x\n\r", byte);
866 switch (iso_state) { /* Handle byte depending on state */
867 case ISO7816_S_RESET: /* During reset we shouldn't receive any data */
868 break;
869 case ISO7816_S_WAIT_ATR: /* After a reset we expect the ATR */
870 change_state(ISO7816_S_IN_ATR); /* go to next state */
871 case ISO7816_S_IN_ATR: /* More ATR data incoming */
872 process_byte_atr(byte);
873 break;
874 case ISO7816_S_WAIT_TPDU: /* After the ATR we expect TPDU or PPS data */
875 case ISO7816_S_WAIT_PPS_RSP:
876 if (byte == 0xff) {
877 if (ISO7816_S_WAIT_PPS_RSP==iso_state) {
878 change_state(ISO7816_S_IN_PPS_RSP); /* Go to PPS state */
879 } else {
880 change_state(ISO7816_S_IN_PPS_REQ); /* Go to PPS state */
881 }
882 process_byte_pps(byte);
883 break;
884 }
885 case ISO7816_S_IN_TPDU: /* More TPDU data incoming */
886 if (ISO7816_S_WAIT_TPDU==iso_state) {
887 change_state(ISO7816_S_IN_TPDU);
888 }
889 process_byte_tpdu(byte);
890 break;
891 case ISO7816_S_IN_PPS_REQ:
892 case ISO7816_S_IN_PPS_RSP:
893 process_byte_pps(byte);
894 break;
895 default:
896 TRACE_ERROR("Data received in unknown state %u\n\r", iso_state);
897 }
898 }
899
900 /* Handle flags */
901 if (change_flags) { /* WARNING this is not synced with the data buffer handling */
902 }
Christina Quastfb524b92015-02-27 13:39:45 +0100903}
Harald Welte2fb59962016-02-28 12:34:26 +0100904#endif /* HAVE_SNIFFER */