blob: c2b87258bfd17ebebffb50761212e54747b400a1 [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 Redon709a4312018-07-03 16:10:04 +020022 * TODO put common ISO7816-3 code is separate library (and combine clean with iso7816_4)
Kévin Redond7a6de52018-06-11 13:46:35 +020023 */
Kévin Redon36abece2018-06-04 16:30:01 +020024#include "board.h"
25#include "simtrace.h"
26
Harald Welte2fb59962016-02-28 12:34:26 +010027#ifdef HAVE_SNIFFER
28
Christina Quasta90eefa2015-02-24 17:52:29 +010029/*------------------------------------------------------------------------------
30 * Headers
31 *------------------------------------------------------------------------------*/
32
Christina Quasta90eefa2015-02-24 17:52:29 +010033#include <string.h>
Kévin Redon8fa6ff52018-06-25 16:00:33 +020034#include "utils.h"
35#include "iso7816_fidi.h"
Kévin Redonde97fd22018-07-01 18:23:23 +020036/* USB related libraries */
37#include "osmocom/core/linuxlist.h"
38#include "osmocom/core/msgb.h"
39#include "llist_irqsafe.h"
40#include "usb_buf.h"
41#include "simtrace_usb.h"
42#include "simtrace_prot.h"
Christina Quasta90eefa2015-02-24 17:52:29 +010043
44/*------------------------------------------------------------------------------
45 * Internal definitions
46 *------------------------------------------------------------------------------*/
47
Kévin Redon8fa6ff52018-06-25 16:00:33 +020048/*! Maximum Answer-To-Reset (ATR) size in bytes
Kévin Redond7a6de52018-06-11 13:46:35 +020049 * @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
50 * @remark technical there is no size limitation since Yi present in T0,TDi will indicate if more interface bytes are present, including TDi+i
51 */
52#define MAX_ATR_SIZE 33
Kévin Redon8fa6ff52018-06-25 16:00:33 +020053/*! Maximum Protocol and Parameters Selection (PPS) size in bytes
54 * @note defined in ISO/IEC 7816-3:2006(E) section 9.2
55 */
56#define MAX_PPS_SIZE 6
Kévin Redond7a6de52018-06-11 13:46:35 +020057
58/*! ISO 7816-3 states relevant to the sniff mode */
59enum iso7816_3_sniff_state {
60 ISO7816_S_RESET, /*!< in Reset */
61 ISO7816_S_WAIT_ATR, /*!< waiting for ATR to start */
62 ISO7816_S_IN_ATR, /*!< while we are receiving the ATR */
Kévin Redon00ec89d2018-06-27 16:41:52 +020063 ISO7816_S_WAIT_TPDU, /*!< waiting for start of new TPDU */
64 ISO7816_S_IN_TPDU, /*!< inside a single TPDU */
Kévin Redon8fa6ff52018-06-25 16:00:33 +020065 ISO7816_S_IN_PPS_REQ, /*!< while we are inside the PPS request */
66 ISO7816_S_WAIT_PPS_RSP, /*!< waiting for start of the PPS response */
67 ISO7816_S_IN_PPS_RSP, /*!< while we are inside the PPS request */
Kévin Redond7a6de52018-06-11 13:46:35 +020068};
69
70/*! Answer-To-Reset (ATR) sub-states of ISO7816_S_IN_ATR
71 * @note defined in ISO/IEC 7816-3:2006(E) section 8
72 */
73enum atr_sniff_state {
74 ATR_S_WAIT_TS, /*!< initial byte */
75 ATR_S_WAIT_T0, /*!< format byte */
76 ATR_S_WAIT_TA, /*!< first sub-group interface byte */
77 ATR_S_WAIT_TB, /*!< second sub-group interface byte */
78 ATR_S_WAIT_TC, /*!< third sub-group interface byte */
79 ATR_S_WAIT_TD, /*!< fourth sub-group interface byte */
80 ATR_S_WAIT_HIST, /*!< historical byte */
81 ATR_S_WAIT_TCK, /*!< check byte */
Kévin Redon8fa6ff52018-06-25 16:00:33 +020082};
83
84/*! Protocol and Parameters Selection (PPS) sub-states of ISO7816_S_IN_PTS_REQ/ISO7816_S_IN_PTS_RSP
85 * @note defined in ISO/IEC 7816-3:2006(E) section 9
86 */
87enum pps_sniff_state {
88 PPS_S_WAIT_PPSS, /*!< initial byte */
89 PPS_S_WAIT_PPS0, /*!< format byte */
90 PPS_S_WAIT_PPS1, /*!< first parameter byte */
91 PPS_S_WAIT_PPS2, /*!< second parameter byte */
92 PPS_S_WAIT_PPS3, /*!< third parameter byte */
93 PPS_S_WAIT_PCK, /*!< check byte */
Kévin Redond7a6de52018-06-11 13:46:35 +020094};
Kévin Redon45ad62d2018-06-07 18:56:41 +020095
Kévin Redon00ec89d2018-06-27 16:41:52 +020096/*! Transport Protocol Data Unit (TPDU) sub-states of ISO7816_S_IN_TPDU
97 * @note defined in ISO/IEC 7816-3:2006(E) section 10 and 12
98 * @remark APDUs are formed by one or more command+response TPDUs
99 */
100enum tpdu_sniff_state {
101 TPDU_S_CLA, /*!< class byte */
102 TPDU_S_INS, /*!< instruction byte */
103 TPDU_S_P1, /*!< first parameter byte for the instruction */
104 TPDU_S_P2, /*!< second parameter byte for the instruction */
105 TPDU_S_P3, /*!< third parameter byte encoding the data length */
106 TPDU_S_PROCEDURE, /*!< procedure byte (could also be SW1) */
107 TPDU_S_DATA_REMAINING, /*!< remaining data bytes */
108 TPDU_S_DATA_SINGLE, /*!< single data byte */
109 TPDU_S_SW1, /*!< first status word */
110 TPDU_S_SW2, /*!< second status word */
111};
112
Christina Quasta90eefa2015-02-24 17:52:29 +0100113/*------------------------------------------------------------------------------
114 * Internal variables
115 *------------------------------------------------------------------------------*/
Kévin Redond7a6de52018-06-11 13:46:35 +0200116
117/* note: the sniffer code is currently designed to support only one sniffing interface, but the hardware would support a second one.
118 * to support a second sniffer interface the code should be restructured to use handles.
119 */
120/* Pin configurations */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200121/*! Pin configuration to sniff communication (using USART connection card) */
Kévin Redonee62a9d2018-06-11 13:42:23 +0200122static const Pin pins_sniff[] = { PINS_SIM_SNIFF };
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200123/*! Pin configuration to interconnect phone and card using the bus switch */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100124static const Pin pins_bus[] = { PINS_BUS_SNIFF };
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200125/*! Pin configuration to power the card by the phone */
Kévin Redond7a6de52018-06-11 13:46:35 +0200126static const Pin pins_power[] = { PINS_PWR_SNIFF };
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200127/*! Pin configuration for timer counter to measure ETU timing */
Kévin Redon45ad62d2018-06-07 18:56:41 +0200128static const Pin pins_tc[] = { PINS_TC };
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200129/*! Pin configuration for card reset line */
130static const Pin pin_rst = PIN_SIM_RST_SNIFF;
131
Kévin Redond7a6de52018-06-11 13:46:35 +0200132/* USART related variables */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200133/*! USART peripheral used to sniff communication */
Kévin Redon45ad62d2018-06-07 18:56:41 +0200134static struct Usart_info sniff_usart = {
135 .base = USART_SIM,
136 .id = ID_USART_SIM,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100137 .state = USART_RCV,
138};
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200139/*! Ring buffer to store sniffer communication data */
Kévin Redon7b734622018-06-06 16:13:48 +0200140static struct ringbuf sniff_buffer;
141
Kévin Redonde97fd22018-07-01 18:23:23 +0200142/* Flags to know is the card status changed (see SIMTRACE_MSGT_DT_SNIFF_CHANGE flags) */
143volatile uint32_t change_flags = 0;
144
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200145/* ISO 7816 variables */
146/*! ISO 7816-3 state */
147enum iso7816_3_sniff_state iso_state = ISO7816_S_RESET;
148/*! ATR state */
149enum atr_sniff_state atr_state;
150/*! ATR data
151 * @remark can be used to check later protocol changes
152 */
153uint8_t atr[MAX_ATR_SIZE];
154/*! Current index in the ATR data */
155uint8_t atr_i = 0;
156/*! If convention conversion is needed */
157bool convention_convert = false;
158/*! The supported T protocols */
159uint16_t t_protocol_support = 0;
160/*! PPS state
161 * @remark it is shared between request and response since they aren't simultaneous but follow the same procedure
162 */
163enum pps_sniff_state pps_state;
164/*! PPS request data
165 * @remark can be used to check PPS response
166 */
167uint8_t pps_req[MAX_PPS_SIZE];
168/*! PPS response data */
169uint8_t pps_rsp[MAX_PPS_SIZE];
Kévin Redon00ec89d2018-06-27 16:41:52 +0200170/*! TPDU state */
171enum tpdu_sniff_state tpdu_state;
172/*! Final TPDU packet
173 * @note this is the complete command+response TPDU, including header, data, and status words
174 * @remark this does not include the procedure bytes
175 */
176uint8_t tpdu_packet[5+256+2];
177/*! Current index in TPDU packet */
178uint8_t tpdu_packet_i = 0;
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200179
Kévin Redon7b734622018-06-06 16:13:48 +0200180/*------------------------------------------------------------------------------
Kévin Redon7b734622018-06-06 16:13:48 +0200181 * Internal functions
182 *------------------------------------------------------------------------------*/
Kévin Redon36abece2018-06-04 16:30:01 +0200183
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200184/*! Convert data between direct and inverse convention
185 * @note direct convention is LSb first and HIGH=1; inverse conversion in MSb first and LOW=1
186 * @remark use a look up table to speed up conversion
187 */
188static 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, };
189
Kévin Redonde97fd22018-07-01 18:23:23 +0200190/*! Allocate USB buffer and push + initialize simtrace_msg_hdr
191 * @param[in] ep USB IN endpoint where the message will be sent to
192 * @param[in] msg_class SIMtrace USB message class
193 * @param[in] msg_type SIMtrace USB message type
194 * @return USB message with allocated ans initialized header, or NULL if allocation failed
195 */
196static struct msgb *usb_msg_alloc_hdr(uint8_t ep, uint8_t msg_class, uint8_t msg_type)
197{
198 struct msgb *usb_msg = usb_buf_alloc(SIMTRACE_USB_EP_CARD_DATAIN);
199 if (!usb_msg) {
200 return NULL;
201 }
202 struct simtrace_msg_hdr *usb_msg_header;
203 usb_msg->l1h = msgb_put(usb_msg, sizeof(*usb_msg_header));
204 usb_msg_header = (struct simtrace_msg_hdr *) usb_msg->l1h;
205 memset(usb_msg_header, 0, sizeof(*usb_msg_header));
206 usb_msg_header->msg_class = SIMTRACE_MSGC_SNIFF;
207 usb_msg_header->msg_type = SIMTRACE_MSGT_SNIFF_CHANGE;
208 usb_msg->l2h = usb_msg->l1h + sizeof(*usb_msg_header);
209
210 return usb_msg;
211}
212
213/* update SIMtrace header msg_len and submit USB buffer
214 * param[in] usb_msg USB message to update and send
215 */
216void usb_msg_upd_len_and_submit(struct msgb *usb_msg)
217{
218 struct simtrace_msg_hdr *usb_msg_header = (struct simtrace_msg_hdr *) usb_msg->l1h;
219 usb_msg_header->msg_len = msgb_length(usb_msg);
220 usb_buf_submit(usb_msg);
221}
222
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200223/*! Update the ISO 7816-3 state
224 * @param[in] iso_state_new new ISO 7816-3 state to update to
225 */
226static void change_state(enum iso7816_3_sniff_state iso_state_new)
227{
228 /* sanity check */
229 if (iso_state_new==iso_state) {
230 TRACE_WARNING("Already in ISO 7816 state %u\n\r", iso_state);
231 return;
232 }
233
234 /* handle actions to perform when switching state */
235 switch (iso_state_new) {
236 case ISO7816_S_RESET:
237 update_fidi(&sniff_usart, 0x11); /* reset baud rate to default Di/Fi values */
Kévin Redon709a4312018-07-03 16:10:04 +0200238 change_flags |= SNIFF_CHANGE_FLAG_RESET_HOLD; /* set flag and let main loop send it */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200239 break;
240 case ISO7816_S_WAIT_ATR:
241 rbuf_reset(&sniff_buffer); /* reset buffer for new communication */
Kévin Redon709a4312018-07-03 16:10:04 +0200242 change_flags |= SNIFF_CHANGE_FLAG_RESET_RELEASE; /* set flag and let main loop send it */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200243 break;
244 case ISO7816_S_IN_ATR:
245 atr_i = 0;
246 convention_convert = false;
247 t_protocol_support = 0;
248 atr_state = ATR_S_WAIT_TS;
249 break;
250 case ISO7816_S_IN_PPS_REQ:
251 case ISO7816_S_IN_PPS_RSP:
252 pps_state = PPS_S_WAIT_PPSS;
253 break;
Kévin Redon00ec89d2018-06-27 16:41:52 +0200254 case ISO7816_S_WAIT_TPDU:
255 tpdu_state = TPDU_S_CLA;
256 tpdu_packet_i = 0;
257 break;
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200258 default:
259 break;
260 }
261
262 /* save new state */
263 iso_state = iso_state_new;
Kévin Redoncf599192018-06-27 16:38:31 +0200264 //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 */
265}
266
Kévin Redonde97fd22018-07-01 18:23:23 +0200267/*! Send current ATR over USB
268 * @note Also print the ATR over serial
269 */
270static void usb_send_atr(void)
Kévin Redoncf599192018-06-27 16:38:31 +0200271{
Kévin Redonde97fd22018-07-01 18:23:23 +0200272 /* Check state */
Kévin Redoncf599192018-06-27 16:38:31 +0200273 if (ISO7816_S_IN_ATR!=iso_state) {
274 TRACE_WARNING("Can't print ATR in ISO 7816-3 state %u\n\r", iso_state);
275 return;
276 }
Kévin Redonde97fd22018-07-01 18:23:23 +0200277 if (atr_i>=ARRAY_SIZE(atr)) {
278 TRACE_ERROR("ATR buffer overflow\n\r");
279 return;
280 }
Kévin Redoncf599192018-06-27 16:38:31 +0200281
Kévin Redonde97fd22018-07-01 18:23:23 +0200282 /* Show activity on LED */
Kévin Redoncf599192018-06-27 16:38:31 +0200283 led_blink(LED_GREEN, BLINK_2O_F);
Kévin Redonde97fd22018-07-01 18:23:23 +0200284
285 /* Print ATR */
Kévin Redoncf599192018-06-27 16:38:31 +0200286 printf("ATR: ");
287 uint8_t i;
Kévin Redonde97fd22018-07-01 18:23:23 +0200288 for (i = 0; i < atr_i; i++) {
Kévin Redoncf599192018-06-27 16:38:31 +0200289 printf("%02x ", atr[i]);
290 }
291 printf("\n\r");
Kévin Redonde97fd22018-07-01 18:23:23 +0200292
293 /* Send ATR over USB */
294 struct msgb *usb_msg = usb_msg_alloc_hdr(SIMTRACE_USB_EP_CARD_DATAIN, SIMTRACE_MSGC_SNIFF, SIMTRACE_MSGT_SNIFF_ATR);
295 if (!usb_msg) {
296 return;
297 }
298 struct sniff_data *usb_sniff_data_atr = (struct sniff_data *) msgb_put(usb_msg, sizeof(*usb_sniff_data_atr));
299 usb_sniff_data_atr->complete = true;
300 usb_sniff_data_atr->length = atr_i;
301 uint8_t *data = msgb_put(usb_msg, usb_sniff_data_atr->length);
302 memcpy(data, atr, atr_i);
303 usb_msg_upd_len_and_submit(usb_msg);
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200304}
305
306/*! Process ATR byte
307 * @param[in] byte ATR byte to process
308 */
309static void process_byte_atr(uint8_t byte)
310{
311 static uint8_t atr_hist_len = 0; /* store the number of expected historical bytes */
312 static uint8_t y = 0; /* last mask of the upcoming TA, TB, TC, TD interface bytes */
313
314 /* sanity check */
315 if (ISO7816_S_IN_ATR!=iso_state) {
316 TRACE_ERROR("Processing ATR data in wrong ISO 7816-3 state %u\n\r", iso_state);
317 return;
318 }
319 if (atr_i>=ARRAY_SIZE(atr)) {
Kévin Redoncf599192018-06-27 16:38:31 +0200320 TRACE_ERROR("ATR data overflow\n\r");
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200321 return;
322 }
323
324 /* save data for use by other functions */
325 atr[atr_i++] = byte;
326
327 /* handle ATR byte depending on current state */
328 switch (atr_state) {
329 case ATR_S_WAIT_TS: /* see ISO/IEC 7816-3:2006 section 8.1 */
330 switch (byte) {
331 case 0x23: /* direct convention used, but decoded using inverse convention (a parity error should also have occurred) */
332 case 0x30: /* inverse convention used, but decoded using direct convention (a parity error should also have occurred) */
333 convention_convert = !convention_convert;
334 case 0x3b: /* direct convention used and correctly decoded */
335 case 0x3f: /* inverse convention used and correctly decoded */
336 atr_state = ATR_S_WAIT_T0; /* wait for format byte */
337 break;
338 default:
339 atr_i--; /* revert last byte */
340 TRACE_WARNING("Invalid TS received\n\r");
341 }
342 break;
343 case ATR_S_WAIT_T0: /* see ISO/IEC 7816-3:2006 section 8.2.2 */
344 case ATR_S_WAIT_TD: /* see ISO/IEC 7816-3:2006 section 8.2.3 */
345 if (ATR_S_WAIT_T0==atr_state) {
346 atr_hist_len = (byte&0x0f); /* save the number of historical bytes */
347 } else if (ATR_S_WAIT_TD==atr_state) {
348 t_protocol_support |= (1<<(byte&0x0f)); /* remember supported protocol to know if TCK will be present */
349 }
350 y = (byte&0xf0); /* remember upcoming interface bytes */
351 if (y&0x10) {
352 atr_state = ATR_S_WAIT_TA; /* wait for interface byte TA */
353 break;
354 }
355 case ATR_S_WAIT_TA: /* see ISO/IEC 7816-3:2006 section 8.2.3 */
356 if (y&0x20) {
357 atr_state = ATR_S_WAIT_TB; /* wait for interface byte TB */
358 break;
359 }
360 case ATR_S_WAIT_TB: /* see ISO/IEC 7816-3:2006 section 8.2.3 */
361 if (y&0x40) {
362 atr_state = ATR_S_WAIT_TC; /* wait for interface byte TC */
363 break;
364 }
365 case ATR_S_WAIT_TC: /* see ISO/IEC 7816-3:2006 section 8.2.3 */
366 if (y&0x80) {
367 atr_state = ATR_S_WAIT_TD; /* wait for interface byte TD */
368 break;
369 } else if (atr_hist_len) {
370 atr_state = ATR_S_WAIT_HIST; /* wait for historical bytes */
371 break;
372 }
373 case ATR_S_WAIT_HIST: /* see ISO/IEC 7816-3:2006 section 8.2.4 */
374 if (atr_hist_len) {
375 atr_hist_len--;
376 }
377 if (0==atr_hist_len) {
378 if (t_protocol_support>1) {
379 atr_state = ATR_S_WAIT_TCK; /* wait for check bytes */
380 break;
381 }
382 } else {
383 break;
384 }
385 case ATR_S_WAIT_TCK: /* see ISO/IEC 7816-3:2006 section 8.2.5 */
386 /* we could verify the checksum, but we are just here to sniff */
Kévin Redonde97fd22018-07-01 18:23:23 +0200387 usb_send_atr(); /* send ATR to host software using USB */
Kévin Redon00ec89d2018-06-27 16:41:52 +0200388 change_state(ISO7816_S_WAIT_TPDU); /* go to next state */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200389 break;
390 default:
391 TRACE_INFO("Unknown ATR state %u\n\r", atr_state);
392 }
393}
394
Kévin Redonde97fd22018-07-01 18:23:23 +0200395/*! Send current PPS over USB
396 * @note Also print the PPS over serial
397 */
398static void usb_send_pps(void)
Kévin Redoncf599192018-06-27 16:38:31 +0200399{
400 uint8_t *pps_cur; /* current PPS (request or response) */
401
Kévin Redonde97fd22018-07-01 18:23:23 +0200402 /* Sanity check */
Kévin Redoncf599192018-06-27 16:38:31 +0200403 if (ISO7816_S_IN_PPS_REQ==iso_state) {
404 pps_cur = pps_req;
405 } else if (ISO7816_S_IN_PPS_RSP==iso_state) {
406 pps_cur = pps_rsp;
407 } else {
408 TRACE_ERROR("Can't print PPS in ISO 7816-3 state %u\n\r", iso_state);
409 return;
410 }
411
Kévin Redonde97fd22018-07-01 18:23:23 +0200412 /* Get only relevant data */
413 uint8_t pps[6];
414 uint8_t pps_i = 0;
415 pps[pps_i++] = pps_cur[0];
416 pps[pps_i++] = pps_cur[1];
Kévin Redoncf599192018-06-27 16:38:31 +0200417 if (pps_cur[1]&0x10) {
Kévin Redonde97fd22018-07-01 18:23:23 +0200418 pps[pps_i++] = pps_cur[2];
Kévin Redoncf599192018-06-27 16:38:31 +0200419 }
420 if (pps_cur[1]&0x20) {
Kévin Redonde97fd22018-07-01 18:23:23 +0200421 pps[pps_i++] = pps_cur[3];
Kévin Redoncf599192018-06-27 16:38:31 +0200422 }
423 if (pps_cur[1]&0x40) {
Kévin Redonde97fd22018-07-01 18:23:23 +0200424 pps[pps_i++] = pps_cur[4];
Kévin Redoncf599192018-06-27 16:38:31 +0200425 }
Kévin Redonde97fd22018-07-01 18:23:23 +0200426 pps[pps_i++] = pps_cur[5];
427
428 /* Show activity on LED */
429 led_blink(LED_GREEN, BLINK_2O_F);
430
431 /* Print PPS */
432 printf("PPS: ");
433 uint8_t i;
434 for (i = 0; i < pps_i; i++) {
435 printf("%02x ", pps[i]);
436 }
Kévin Redoncf599192018-06-27 16:38:31 +0200437 printf("\n\r");
Kévin Redonde97fd22018-07-01 18:23:23 +0200438
439 /* Send message over USB */
440 struct msgb *usb_msg = usb_msg_alloc_hdr(SIMTRACE_USB_EP_CARD_DATAIN, SIMTRACE_MSGC_SNIFF, SIMTRACE_MSGT_SNIFF_PPS);
441 if (!usb_msg) {
442 return;
443 }
444 struct sniff_data *usb_sniff_data_pps = (struct sniff_data *) msgb_put(usb_msg, sizeof(*usb_sniff_data_pps));
445 usb_sniff_data_pps->complete = true;
446 usb_sniff_data_pps->length = pps_i;
447 uint8_t *data = msgb_put(usb_msg, usb_sniff_data_pps->length);
448 memcpy(data, pps, pps_i);
449 usb_msg_upd_len_and_submit(usb_msg);
450}
451
452/*! Send Fi/Di change over USB
453 * @param[in] fidi Fi/Di factor as encoded in TA1
454 */
455static void usb_send_fidi(uint8_t fidi)
456{
457 /* Send message over USB */
458 struct msgb *usb_msg = usb_msg_alloc_hdr(SIMTRACE_USB_EP_CARD_DATAIN, SIMTRACE_MSGC_SNIFF, SIMTRACE_MSGT_SNIFF_FIDI);
459 if (!usb_msg) {
460 return;
461 }
462 struct sniff_fidi *usb_sniff_fidi = (struct sniff_fidi *) msgb_put(usb_msg, sizeof(*usb_sniff_fidi));
463 usb_sniff_fidi->fidi = fidi;
464 usb_msg_upd_len_and_submit(usb_msg);
Kévin Redoncf599192018-06-27 16:38:31 +0200465}
466
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200467static void process_byte_pps(uint8_t byte)
468{
469 uint8_t *pps_cur; /* current PPS (request or response) */
470
471 /* sanity check */
472 if (ISO7816_S_IN_PPS_REQ==iso_state) {
473 pps_cur = pps_req;
474 } else if (ISO7816_S_IN_PPS_RSP==iso_state) {
475 pps_cur = pps_rsp;
476 } else {
477 TRACE_ERROR("Processing PPS data in wrong ISO 7816-3 state %u\n\r", iso_state);
478 return;
479 }
480
481 /* handle PPS byte depending on current state */
482 switch (pps_state) { /* see ISO/IEC 7816-3:2006 section 9.2 */
483 case PPS_S_WAIT_PPSS: /*!< initial byte */
484 if (0xff) {
485 pps_cur[0] = byte;
486 pps_state = PPS_S_WAIT_PPS0; /* go to next state */
487 } else {
488 TRACE_INFO("Invalid PPSS received\n\r");
Kévin Redon00ec89d2018-06-27 16:41:52 +0200489 change_state(ISO7816_S_WAIT_TPDU); /* go back to TPDU state */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200490 }
491 break;
492 case PPS_S_WAIT_PPS0: /*!< format byte */
493 pps_cur[1] = byte;
494 if (pps_cur[1]&0x10) {
495 pps_state = PPS_S_WAIT_PPS1; /* go to next state */
496 break;
497 }
498 case PPS_S_WAIT_PPS1: /*!< first parameter byte */
499 pps_cur[2] = byte; /* not always right but doesn't affect the process */
500 if (pps_cur[1]&0x20) {
501 pps_state = PPS_S_WAIT_PPS2; /* go to next state */
502 break;
503 }
504 case PPS_S_WAIT_PPS2: /*!< second parameter byte */
505 pps_cur[3] = byte; /* not always right but doesn't affect the process */
506 if (pps_cur[1]&0x40) {
507 pps_state = PPS_S_WAIT_PPS3; /* go to next state */
508 break;
509 }
510 case PPS_S_WAIT_PPS3: /*!< third parameter byte */
511 pps_cur[4] = byte; /* not always right but doesn't affect the process */
512 pps_state = PPS_S_WAIT_PCK; /* go to next state */
513 break;
514 case PPS_S_WAIT_PCK: /*!< check byte */
515 pps_cur[5] = byte; /* not always right but doesn't affect the process */
516 /* verify the checksum */
517 uint8_t check = 0;
518 check ^= pps_cur[0];
519 check ^= pps_cur[1];
520 if (pps_cur[1]&0x10) {
521 check ^= pps_cur[2];
522 }
523 if (pps_cur[1]&0x20) {
524 check ^= pps_cur[3];
525 }
526 if (pps_cur[1]&0x40) {
527 check ^= pps_cur[4];
528 }
529 check ^= pps_cur[5];
Kévin Redonde97fd22018-07-01 18:23:23 +0200530 usb_send_pps(); /* send PPS to host software using USB */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200531 if (ISO7816_S_IN_PPS_REQ==iso_state) {
532 if (0==check) { /* checksum is valid */
533 change_state(ISO7816_S_WAIT_PPS_RSP); /* go to next state */
534 } else { /* checksum is invalid */
Kévin Redon00ec89d2018-06-27 16:41:52 +0200535 change_state(ISO7816_S_WAIT_TPDU); /* go to next state */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200536 }
537 } else if (ISO7816_S_IN_PPS_RSP==iso_state) {
538 if (0==check) { /* checksum is valid */
539 uint8_t fn, dn;
540 if (pps_cur[1]&0x10) {
541 fn = (pps_cur[2]>>4);
542 dn = (pps_cur[2]&0x0f);
543 } else {
544 fn = 1;
545 dn = 1;
546 }
Kévin Redon74063372018-07-03 15:57:03 +0200547 TRACE_INFO("PPS negotiation successful: Fn=%u Dn=%u\n\r", fi_table[fn], di_table[dn]);
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200548 update_fidi(&sniff_usart, pps_cur[2]);
Kévin Redonde97fd22018-07-01 18:23:23 +0200549 usb_send_fidi(pps_cur[2]); /* send Fi/Di change notification to host software over USB */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200550 } else { /* checksum is invalid */
551 TRACE_INFO("PPS negotiation failed\n\r");
552 }
Kévin Redon00ec89d2018-06-27 16:41:52 +0200553 change_state(ISO7816_S_WAIT_TPDU); /* go to next state */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200554 }
555 break;
556 default:
557 TRACE_INFO("Unknown PPS state %u\n\r", pps_state);
558 }
559}
560
Kévin Redonde97fd22018-07-01 18:23:23 +0200561/*! Send current TPDU over USB
562 * @note Also print the TPDU over serial
563 */
564static void usb_send_tpdu(void)
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200565{
Kévin Redonde97fd22018-07-01 18:23:23 +0200566 /* Check state */
Kévin Redon00ec89d2018-06-27 16:41:52 +0200567 if (ISO7816_S_IN_TPDU!=iso_state) {
568 TRACE_WARNING("Can't print TPDU in ISO 7816-3 state %u\n\r", iso_state);
569 return;
570 }
571
Kévin Redonde97fd22018-07-01 18:23:23 +0200572 /* Show activity on LED */
Kévin Redon00ec89d2018-06-27 16:41:52 +0200573 led_blink(LED_GREEN, BLINK_2O_F);
Kévin Redonde97fd22018-07-01 18:23:23 +0200574
575 /* Print TPDU */
Kévin Redon00ec89d2018-06-27 16:41:52 +0200576 printf("TPDU: ");
577 uint16_t i;
578 for (i = 0; i < tpdu_packet_i && i < ARRAY_SIZE(tpdu_packet); i++) {
579 printf("%02x ", tpdu_packet[i]);
580 }
581 printf("\n\r");
Kévin Redonde97fd22018-07-01 18:23:23 +0200582
583 /* Send ATR over USB */
584 struct msgb *usb_msg = usb_msg_alloc_hdr(SIMTRACE_USB_EP_CARD_DATAIN, SIMTRACE_MSGC_SNIFF, SIMTRACE_MSGT_SNIFF_TPDU);
585 if (!usb_msg) {
586 return;
587 }
588 struct sniff_data *usb_sniff_data_tpdu = (struct sniff_data *) msgb_put(usb_msg, sizeof(*usb_sniff_data_tpdu));
589 usb_sniff_data_tpdu->complete = true;
590 usb_sniff_data_tpdu->length = tpdu_packet_i;
591 uint8_t *data = msgb_put(usb_msg, usb_sniff_data_tpdu->length);
592 memcpy(data, tpdu_packet, tpdu_packet_i);
593 usb_msg_upd_len_and_submit(usb_msg);
Kévin Redon00ec89d2018-06-27 16:41:52 +0200594}
595
596static void process_byte_tpdu(uint8_t byte)
597{
598 /* sanity check */
599 if (ISO7816_S_IN_TPDU!=iso_state) {
600 TRACE_ERROR("Processing TPDU data in wrong ISO 7816-3 state %u\n\r", iso_state);
601 return;
602 }
603 if (tpdu_packet_i>=ARRAY_SIZE(tpdu_packet)) {
604 TRACE_ERROR("TPDU data overflow\n\r");
605 return;
606 }
607
608 /* handle TPDU byte depending on current state */
609 switch (tpdu_state) {
610 case TPDU_S_CLA:
611 if (0xff==byte) {
612 TRACE_WARNING("0xff is not a valid class byte\n\r");
613 break;
614 }
615 tpdu_packet_i = 0;
616 tpdu_packet[tpdu_packet_i++] = byte;
617 tpdu_state = TPDU_S_INS;
618 break;
619 case TPDU_S_INS:
620 tpdu_packet_i = 1;
621 tpdu_packet[tpdu_packet_i++] = byte;
622 tpdu_state = TPDU_S_P1;
623 break;
624 case TPDU_S_P1:
625 tpdu_packet_i = 2;
626 tpdu_packet[tpdu_packet_i++] = byte;
627 tpdu_state = TPDU_S_P2;
628 break;
629 case TPDU_S_P2:
630 tpdu_packet_i = 3;
631 tpdu_packet[tpdu_packet_i++] = byte;
632 tpdu_state = TPDU_S_P3;
633 break;
634 case TPDU_S_P3:
635 tpdu_packet_i = 4;
636 tpdu_packet[tpdu_packet_i++] = byte;
637 tpdu_state = TPDU_S_PROCEDURE;
638 break;
639 case TPDU_S_PROCEDURE:
640 if (0x60==byte) { /* wait for next procedure byte */
641 break;
642 } else if (tpdu_packet[1]==byte) { /* get all remaining data bytes */
643 tpdu_state = TPDU_S_DATA_REMAINING;
644 break;
645 } else if ((~tpdu_packet[1])==byte) { /* get single data byte */
646 tpdu_state = TPDU_S_DATA_SINGLE;
647 break;
648 }
649 case TPDU_S_SW1:
650 if ((0x60==(byte&0xf0)) || (0x90==(byte&0xf0))) { /* this procedure byte is SW1 */
651 tpdu_packet[tpdu_packet_i++] = byte;
652 tpdu_state = TPDU_S_SW2;
653 } else {
654 TRACE_WARNING("invalid SW1 0x%02x\n\r", byte);
655 }
656 break;
657 case TPDU_S_SW2:
658 tpdu_packet[tpdu_packet_i++] = byte;
Kévin Redonde97fd22018-07-01 18:23:23 +0200659 usb_send_tpdu(); /* send TPDU to host software using USB */
Kévin Redon00ec89d2018-06-27 16:41:52 +0200660 change_state(ISO7816_S_WAIT_TPDU); /* this is the end of the TPDU */
661 break;
662 case TPDU_S_DATA_SINGLE:
663 case TPDU_S_DATA_REMAINING:
664 tpdu_packet[tpdu_packet_i++] = byte;
665 if (0==tpdu_packet[4]) {
666 if (5+256<=tpdu_packet_i) {
667 tpdu_state = TPDU_S_SW1;
668 }
669 } else {
670 if (5+tpdu_packet[4]<=tpdu_packet_i) {
671 tpdu_state = TPDU_S_SW1;
672 }
673 }
674 if (TPDU_S_DATA_SINGLE==tpdu_state) {
675 tpdu_state = TPDU_S_PROCEDURE;
676 }
677 break;
678 default:
679 TRACE_ERROR("unhandled TPDU state %u\n\r", tpdu_state);
680 }
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200681}
682
Kévin Redond7a6de52018-06-11 13:46:35 +0200683/*! Interrupt Service Routine called on USART activity */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200684void Sniffer_usart_isr(void)
Kévin Redond7a6de52018-06-11 13:46:35 +0200685{
686 /* Read channel status register */
Kévin Redon709a4312018-07-03 16:10:04 +0200687 uint32_t csr = sniff_usart.base->US_CSR;
688 /* Verify if there was an error */
689 if (csr & US_CSR_OVRE) {
690 TRACE_WARNING("USART overrun error\n\r");
691 sniff_usart.base->US_CR |= US_CR_RSTSTA;
692 }
693 if (csr & US_CSR_FRAME) {
694 TRACE_WARNING("USART framing error\n\r");
695 sniff_usart.base->US_CR |= US_CR_RSTSTA;
696 }
Kévin Redond7a6de52018-06-11 13:46:35 +0200697 /* Verify if character has been received */
698 if (csr & US_CSR_RXRDY) {
699 /* Read communication data byte between phone and SIM */
700 uint8_t byte = sniff_usart.base->US_RHR;
701 /* Store sniffed data into buffer (also clear interrupt */
Kévin Redon709a4312018-07-03 16:10:04 +0200702 if (rbuf_is_full(&sniff_buffer)) {
703 TRACE_ERROR("USART buffer full\n\r");
704 } else {
705 rbuf_write(&sniff_buffer, byte);
706 }
Kévin Redond7a6de52018-06-11 13:46:35 +0200707 }
Kévin Redon709a4312018-07-03 16:10:04 +0200708
Kévin Redon638cec82018-06-27 16:42:56 +0200709 /* Verify it WT timeout occurred, to detect unresponsive card */
710 if (csr & US_CSR_TIMEOUT) {
711 /* Stop timeout until next character is received */
712 sniff_usart.base->US_CR |= US_CR_STTTO;
Kévin Redon709a4312018-07-03 16:10:04 +0200713 /* Just set the flag and let the main loop handle it */
714 change_flags |= SNIFF_CHANGE_FLAG_TIMEOUT_WT;
Kévin Redon638cec82018-06-27 16:42:56 +0200715 }
Kévin Redond7a6de52018-06-11 13:46:35 +0200716}
717
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200718/** PIO interrupt service routine to checks if the card reset line has changed
719 */
720static void Sniffer_reset_isr(const Pin* pPin)
721{
722 /* Ensure an edge on the reset pin cause the interrupt */
723 if (pPin->id!=pin_rst.id || 0==(pPin->mask&pin_rst.mask)) {
724 TRACE_ERROR("Pin other than reset caused a interrupt\n\r");
725 return;
726 }
727 /* Update the ISO state according to the reset change */
728 if (PIO_Get(&pin_rst)) {
729 if (ISO7816_S_WAIT_ATR!=iso_state) {
730 change_state(ISO7816_S_WAIT_ATR);
731 }
732 } else {
733 if (ISO7816_S_RESET!=iso_state) {
734 change_state(ISO7816_S_RESET);
735 }
736 }
737}
738
Kévin Redond7a6de52018-06-11 13:46:35 +0200739/*------------------------------------------------------------------------------
740 * Global functions
741 *------------------------------------------------------------------------------*/
742
743void Sniffer_usart1_irq(void)
744{
745 if (ID_USART1==sniff_usart.id) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200746 Sniffer_usart_isr();
Kévin Redond7a6de52018-06-11 13:46:35 +0200747 }
748}
749
750void Sniffer_usart0_irq(void)
751{
752 if (ID_USART0==sniff_usart.id) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200753 Sniffer_usart_isr();
Kévin Redond7a6de52018-06-11 13:46:35 +0200754 }
755}
756
Christina Quasta90eefa2015-02-24 17:52:29 +0100757/*-----------------------------------------------------------------------------
Christina Quastd2b05f02015-02-25 18:44:24 +0100758 * Initialization routine
Christina Quasta90eefa2015-02-24 17:52:29 +0100759 *-----------------------------------------------------------------------------*/
Christina Quasta90eefa2015-02-24 17:52:29 +0100760
Harald Welteed75c622017-11-28 21:23:12 +0100761/* Called during USB enumeration after device is enumerated by host */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100762void Sniffer_configure(void)
Christina Quasta90eefa2015-02-24 17:52:29 +0100763{
Kévin Redon36abece2018-06-04 16:30:01 +0200764 TRACE_INFO("Sniffer config\n\r");
Christina Quasta90eefa2015-02-24 17:52:29 +0100765}
Christina Quastfb524b92015-02-27 13:39:45 +0100766
Harald Welteed75c622017-11-28 21:23:12 +0100767/* called when *different* configuration is set by host */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100768void Sniffer_exit(void)
Christina Quastfb524b92015-02-27 13:39:45 +0100769{
Kévin Redon36abece2018-06-04 16:30:01 +0200770 TRACE_INFO("Sniffer exit\n\r");
Kévin Redonde97fd22018-07-01 18:23:23 +0200771 /* Disable USART */
Kévin Redon45ad62d2018-06-07 18:56:41 +0200772 USART_DisableIt(sniff_usart.base, US_IER_RXRDY);
773 /* NOTE: don't forget to set the IRQ according to the USART peripheral used */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200774 NVIC_DisableIRQ(IRQ_USART_SIM);
Kévin Redon45ad62d2018-06-07 18:56:41 +0200775 USART_SetReceiverEnabled(sniff_usart.base, 0);
Kévin Redonde97fd22018-07-01 18:23:23 +0200776 /* Disable RST IRQ */
777 PIO_DisableIt(&pin_rst);
778 NVIC_DisableIRQ(PIOA_IRQn); /* CAUTION this needs to match to the correct port */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100779}
780
Kévin Redon36abece2018-06-04 16:30:01 +0200781/* called when *Sniffer* configuration is set by host */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100782void Sniffer_init(void)
783{
Kévin Redon36abece2018-06-04 16:30:01 +0200784 TRACE_INFO("Sniffer Init\n\r");
Kévin Redon7b734622018-06-06 16:13:48 +0200785
Kévin Redon45ad62d2018-06-07 18:56:41 +0200786 /* Configure pins to sniff communication between phone and card */
787 PIO_Configure(pins_sniff, PIO_LISTSIZE(pins_sniff));
788 /* Configure pins to connect phone to card */
789 PIO_Configure(pins_bus, PIO_LISTSIZE(pins_bus));
790 /* Configure pins to forward phone power to card */
791 PIO_Configure(pins_power, PIO_LISTSIZE(pins_power));
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200792 /* Enable interrupts on port with reset line */
793 NVIC_EnableIRQ(PIOA_IRQn); /* CAUTION this needs to match to the correct port */
794 /* Register ISR to handle card reset change */
795 PIO_ConfigureIt(&pin_rst, &Sniffer_reset_isr);
796 /* Enable interrupt on card reset pin */
797 PIO_EnableIt(&pin_rst);
Kévin Redon45ad62d2018-06-07 18:56:41 +0200798
Kévin Redon7b734622018-06-06 16:13:48 +0200799 /* Clear ring buffer containing the sniffed data */
800 rbuf_reset(&sniff_buffer);
Kévin Redon45ad62d2018-06-07 18:56:41 +0200801 /* Configure USART to as ISO-7816 slave communication to sniff communication */
802 ISO7816_Init(&sniff_usart, CLK_SLAVE);
803 /* Only receive data when sniffing */
804 USART_SetReceiverEnabled(sniff_usart.base, 1);
Kévin Redon638cec82018-06-27 16:42:56 +0200805 /* Enable Receiver time-out WT to detect unresponsive cards */
806 sniff_usart.base->US_RTOR = 9600-12; /* -12 because the timer starts at the end of the 12 ETU frame */
807 /* Enable interrupt to indicate when data has been received or timeout occurred */
808 USART_EnableIt(sniff_usart.base, US_IER_RXRDY | US_IER_TIMEOUT);
Kévin Redond7a6de52018-06-11 13:46:35 +0200809 /* Enable interrupt requests for the USART peripheral */
810 NVIC_EnableIRQ(IRQ_USART_SIM);
811
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200812 /* Reset state */
813 if (ISO7816_S_RESET!=iso_state) {
814 change_state(ISO7816_S_RESET);
815 }
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100816}
817
Kévin Redon709a4312018-07-03 16:10:04 +0200818/*! Send card change flags over USB
819 * @param[in] flags change flags corresponding to SIMTRACE_MSGT_SNIFF_CHANGE
820 */
821static void usb_send_change(uint32_t flags)
822{
823 /* Check flags */
824 if(0==flags) { /* no changes */
825 return;
826 }
827
828 if (flags&SNIFF_CHANGE_FLAG_TIMEOUT_WT) {
829 printf("waiting time (WT) timeout\n\r");
830 }
831
832 /* Send message over USB */
833 struct msgb *usb_msg = usb_msg_alloc_hdr(SIMTRACE_USB_EP_CARD_DATAIN, SIMTRACE_MSGC_SNIFF, SIMTRACE_MSGT_SNIFF_CHANGE);
834 if (!usb_msg) {
835 return;
836 }
837 struct sniff_change *usb_sniff_change = (struct sniff_change *) msgb_put(usb_msg, sizeof(*usb_sniff_change));
838 usb_sniff_change->flags = flags;
839 usb_msg_upd_len_and_submit(usb_msg);
840}
841
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200842/* Main (idle/busy) loop of this USB configuration */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100843void Sniffer_run(void)
844{
Kévin Redonde97fd22018-07-01 18:23:23 +0200845 /* Handle USB queue */
846 /* first try to send any pending messages on INT */
847 usb_refill_to_host(SIMTRACE_USB_EP_CARD_INT);
848 /* then try to send any pending messages on IN */
849 usb_refill_to_host(SIMTRACE_USB_EP_CARD_DATAIN);
850 /* ensure we can handle incoming USB messages from the host */
851 /* currently we don't need any incoming data
852 usb_refill_from_host(SIMTRACE_USB_EP_CARD_DATAOUT);
853 struct llist_head *queue = usb_get_queue(SIMTRACE_USB_EP_CARD_DATAOUT);
854 process_any_usb_commands(queue);
855 */
856
857 /* Handle sniffed data */
858 if (!rbuf_is_empty(&sniff_buffer)) { /* use if instead of while to let the main loop restart the watchdog */
859 uint8_t byte = rbuf_read(&sniff_buffer);
Kévin Redon709a4312018-07-03 16:10:04 +0200860 /* Convert convention if required */
861 if (convention_convert) {
862 byte = convention_convert_lut[byte];
863 }
864 //TRACE_ERROR_WP(">%02x", byte);
Kévin Redonde97fd22018-07-01 18:23:23 +0200865 switch (iso_state) { /* Handle byte depending on state */
866 case ISO7816_S_RESET: /* During reset we shouldn't receive any data */
867 break;
868 case ISO7816_S_WAIT_ATR: /* After a reset we expect the ATR */
869 change_state(ISO7816_S_IN_ATR); /* go to next state */
870 case ISO7816_S_IN_ATR: /* More ATR data incoming */
871 process_byte_atr(byte);
872 break;
873 case ISO7816_S_WAIT_TPDU: /* After the ATR we expect TPDU or PPS data */
874 case ISO7816_S_WAIT_PPS_RSP:
875 if (byte == 0xff) {
876 if (ISO7816_S_WAIT_PPS_RSP==iso_state) {
877 change_state(ISO7816_S_IN_PPS_RSP); /* Go to PPS state */
878 } else {
879 change_state(ISO7816_S_IN_PPS_REQ); /* Go to PPS state */
880 }
881 process_byte_pps(byte);
882 break;
883 }
884 case ISO7816_S_IN_TPDU: /* More TPDU data incoming */
885 if (ISO7816_S_WAIT_TPDU==iso_state) {
886 change_state(ISO7816_S_IN_TPDU);
887 }
888 process_byte_tpdu(byte);
889 break;
890 case ISO7816_S_IN_PPS_REQ:
891 case ISO7816_S_IN_PPS_RSP:
892 process_byte_pps(byte);
893 break;
894 default:
895 TRACE_ERROR("Data received in unknown state %u\n\r", iso_state);
896 }
897 }
898
899 /* Handle flags */
900 if (change_flags) { /* WARNING this is not synced with the data buffer handling */
Kévin Redon709a4312018-07-03 16:10:04 +0200901 if (change_flags&SNIFF_CHANGE_FLAG_TIMEOUT_WT) {
902 /* Use timeout to detect interrupted data transmission */
903 switch (iso_state) {
904 case ISO7816_S_IN_ATR:
905 //usb_send_atr(false); /* send incomplete ATR to host software using USB */
906 change_state(ISO7816_S_WAIT_ATR);
907 break;
908 case ISO7816_S_IN_TPDU:
909 //usb_send_tpdu(false); /* send incomplete PPS to host software using USB */
910 change_state(ISO7816_S_WAIT_TPDU);
911 break;
912 case ISO7816_S_IN_PPS_REQ:
913 case ISO7816_S_IN_PPS_RSP:
914 //usb_send_pps(false); /* send incomplete TPDU to host software using USB */
915 change_state(ISO7816_S_WAIT_TPDU);
916 break;
917 default:
918 change_flags &= ~SNIFF_CHANGE_FLAG_TIMEOUT_WT; /* We don't care about the timeout is all other cases */
919 break;
920 }
921 }
922 if (change_flags) {
923 usb_send_change(change_flags); /* send timeout to host software over USB */
924 change_flags = 0; /* Reset flags */
925 }
Kévin Redonde97fd22018-07-01 18:23:23 +0200926 }
Christina Quastfb524b92015-02-27 13:39:45 +0100927}
Harald Welte2fb59962016-02-28 12:34:26 +0100928#endif /* HAVE_SNIFFER */