blob: 7cc688b35490aa653a80b3ae9d810eb848c8fd96 [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 Redon36abece2018-06-04 16:30:01 +020020#include "board.h"
21#include "simtrace.h"
22
Harald Welte2fb59962016-02-28 12:34:26 +010023#ifdef HAVE_SNIFFER
24
Christina Quasta90eefa2015-02-24 17:52:29 +010025/*------------------------------------------------------------------------------
26 * Headers
27 *------------------------------------------------------------------------------*/
28
Christina Quasta90eefa2015-02-24 17:52:29 +010029#include <string.h>
30
31/*------------------------------------------------------------------------------
32 * Internal definitions
33 *------------------------------------------------------------------------------*/
34
35/** Maximum ucSize in bytes of the smartcard answer to a command.*/
36#define MAX_ANSWER_SIZE 10
37
38/** Maximum ATR ucSize in bytes.*/
39#define MAX_ATR_SIZE 55
Kévin Redon45ad62d2018-06-07 18:56:41 +020040
Christina Quasta90eefa2015-02-24 17:52:29 +010041/*------------------------------------------------------------------------------
42 * Internal variables
43 *------------------------------------------------------------------------------*/
Kévin Redon45ad62d2018-06-07 18:56:41 +020044/* Pin configuration to sniff communication (using USART connection to SIM card) */
45static const Pin pins_sniff[] = { PINS_SIM_SNIFF_SIM };
46/* Connect phone to card using bus switch */
Harald Welte7dd3dfd2016-03-03 12:32:04 +010047static const Pin pins_bus[] = { PINS_BUS_SNIFF };
Kévin Redon45ad62d2018-06-07 18:56:41 +020048/* Power card using phone VCC */
49static const Pin pins_power[] = { PWR_PINS };
50/* Timer Counter pins to measure ETU timing */
51static const Pin pins_tc[] = { PINS_TC };
52/* USART peripheral used to sniff communication */
53static struct Usart_info sniff_usart = {
54 .base = USART_SIM,
55 .id = ID_USART_SIM,
Harald Welte7dd3dfd2016-03-03 12:32:04 +010056 .state = USART_RCV,
57};
Kévin Redon7b734622018-06-06 16:13:48 +020058/* Ring buffer to store sniffer communication data */
59static struct ringbuf sniff_buffer;
60
61/*------------------------------------------------------------------------------
62 * Global functions
63 *------------------------------------------------------------------------------*/
64
Kévin Redon45ad62d2018-06-07 18:56:41 +020065void Sniffer_usart0_irq(void)
Kévin Redon7b734622018-06-06 16:13:48 +020066{
67 /* Read channel status register */
Kévin Redon45ad62d2018-06-07 18:56:41 +020068 uint32_t csr = sniff_usart.base->US_CSR & sniff_usart.base->US_IMR;
Kévin Redon7b734622018-06-06 16:13:48 +020069 /* Verify if character has been received */
70 if (csr & US_CSR_RXRDY) {
71 /* Read communication data byte between phone and SIM */
Kévin Redon45ad62d2018-06-07 18:56:41 +020072 uint8_t byte = sniff_usart.base->US_RHR;
Kévin Redon7b734622018-06-06 16:13:48 +020073 /* Store sniffed data into buffer (also clear interrupt */
74 rbuf_write(&sniff_buffer, byte);
75 }
76}
77
78/*------------------------------------------------------------------------------
79 * Internal functions
80 *------------------------------------------------------------------------------*/
Kévin Redon36abece2018-06-04 16:30:01 +020081
Kévin Redon45ad62d2018-06-07 18:56:41 +020082static void check_sniffed_data(void)
Kévin Redon36abece2018-06-04 16:30:01 +020083{
Kévin Redon7b734622018-06-06 16:13:48 +020084 /* Display sniffed data */
85 while (!rbuf_is_empty(&sniff_buffer)) {
86 uint8_t byte = rbuf_read(&sniff_buffer);
87 TRACE_INFO_WP("0x%02x ", byte);
88 }
Kévin Redon36abece2018-06-04 16:30:01 +020089}
90
Christina Quasta90eefa2015-02-24 17:52:29 +010091/*-----------------------------------------------------------------------------
Christina Quastd2b05f02015-02-25 18:44:24 +010092 * Initialization routine
Christina Quasta90eefa2015-02-24 17:52:29 +010093 *-----------------------------------------------------------------------------*/
Christina Quasta90eefa2015-02-24 17:52:29 +010094
Harald Welteed75c622017-11-28 21:23:12 +010095/* Called during USB enumeration after device is enumerated by host */
Harald Welte7dd3dfd2016-03-03 12:32:04 +010096void Sniffer_configure(void)
Christina Quasta90eefa2015-02-24 17:52:29 +010097{
Kévin Redon36abece2018-06-04 16:30:01 +020098 TRACE_INFO("Sniffer config\n\r");
Christina Quasta90eefa2015-02-24 17:52:29 +010099}
Christina Quastfb524b92015-02-27 13:39:45 +0100100
Harald Welteed75c622017-11-28 21:23:12 +0100101/* called when *different* configuration is set by host */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100102void Sniffer_exit(void)
Christina Quastfb524b92015-02-27 13:39:45 +0100103{
Kévin Redon36abece2018-06-04 16:30:01 +0200104 TRACE_INFO("Sniffer exit\n\r");
Kévin Redon45ad62d2018-06-07 18:56:41 +0200105 USART_DisableIt(sniff_usart.base, US_IER_RXRDY);
106 /* NOTE: don't forget to set the IRQ according to the USART peripheral used */
107 NVIC_DisableIRQ(USART0_IRQn);
108 USART_SetReceiverEnabled(sniff_usart.base, 0);
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100109}
110
Kévin Redon36abece2018-06-04 16:30:01 +0200111/* called when *Sniffer* configuration is set by host */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100112void Sniffer_init(void)
113{
Kévin Redon36abece2018-06-04 16:30:01 +0200114 TRACE_INFO("Sniffer Init\n\r");
Kévin Redon7b734622018-06-06 16:13:48 +0200115
Kévin Redon45ad62d2018-06-07 18:56:41 +0200116 /* Configure pins to sniff communication between phone and card */
117 PIO_Configure(pins_sniff, PIO_LISTSIZE(pins_sniff));
118 /* Configure pins to connect phone to card */
119 PIO_Configure(pins_bus, PIO_LISTSIZE(pins_bus));
120 /* Configure pins to forward phone power to card */
121 PIO_Configure(pins_power, PIO_LISTSIZE(pins_power));
122
Kévin Redon7b734622018-06-06 16:13:48 +0200123 /* Clear ring buffer containing the sniffed data */
124 rbuf_reset(&sniff_buffer);
Kévin Redon45ad62d2018-06-07 18:56:41 +0200125 /* Configure USART to as ISO-7816 slave communication to sniff communication */
126 ISO7816_Init(&sniff_usart, CLK_SLAVE);
127 /* Only receive data when sniffing */
128 USART_SetReceiverEnabled(sniff_usart.base, 1);
129 /* Enable interrupt to indicate when data has been received */
130 USART_EnableIt(sniff_usart.base, US_IER_RXRDY);
131 /* Enable interrupt requests for the USART peripheral (warning: use IRQ corresponding to USART) */
132 NVIC_EnableIRQ(USART0_IRQn);
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100133}
134
Harald Welteed75c622017-11-28 21:23:12 +0100135/* main (idle/busy) loop of this USB configuration */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100136void Sniffer_run(void)
137{
Kévin Redon45ad62d2018-06-07 18:56:41 +0200138 check_sniffed_data();
Christina Quastfb524b92015-02-27 13:39:45 +0100139}
Harald Welte2fb59962016-02-28 12:34:26 +0100140#endif /* HAVE_SNIFFER */