Renamed simtrace_src to src_simtrace

This commit is contained in:
Christina Quast
2015-04-09 17:07:04 +02:00
parent 590c221893
commit e2dddd5885
13 changed files with 2 additions and 2 deletions

View File

@@ -0,0 +1,186 @@
/* ----------------------------------------------------------------------------
* ATMEL Microcontroller Software Support
* ----------------------------------------------------------------------------
* Copyright (c) 2009, Atmel Corporation
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Atmel's name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ----------------------------------------------------------------------------
*/
/*------------------------------------------------------------------------------
* Headers
*------------------------------------------------------------------------------*/
#include "board.h"
#include <string.h>
/*------------------------------------------------------------------------------
* Internal definitions
*------------------------------------------------------------------------------*/
/** Maximum ucSize in bytes of the smartcard answer to a command.*/
#define MAX_ANSWER_SIZE 10
/** Maximum ATR ucSize in bytes.*/
#define MAX_ATR_SIZE 55
/*------------------------------------------------------------------------------
* Internal variables
*------------------------------------------------------------------------------*/
/** ISO7816 pins */
static const Pin pinsISO7816[] = {PINS_ISO7816};
/** Bus switch pins */
static const Pin pinsBus[] = {PINS_BUS_DEFAULT};
/* SIMcard power pin */
static const Pin pinsPower[] = {PWR_PINS};
/** ISO7816 RST pin */
static const Pin pinIso7816RstMC = PIN_ISO7816_RSTMC;
static uint8_t sim_inserted = 0;
/*------------------------------------------------------------------------------
* Optional smartcard detection
*------------------------------------------------------------------------------*/
#ifdef SMARTCARD_CONNECT_PIN
/** Smartcard detection pin.*/
static const Pin pinSmartCard = SMARTCARD_CONNECT_PIN;
/**
* PIO interrupt service routine. Checks if the smartcard has been connected
* or disconnected.
*/
static void ISR_PioSmartCard( const Pin *pPin )
{
/* FIXME: why is pinSmartCard.pio->PIO_ISR the wrong number?
printf("+++++ Trying to check for pending interrupts (PIO ISR: 0x%X)\n\r", pinSmartCard.pio->PIO_ISR);
printf("+++++ Mask: 0x%X\n\r", pinSmartCard.mask);
Output:
+++++ Trying to check for pending interrupts (PIO ISR: 0x400)) = 1<<10
+++++ Mask: 0x100 = 1<<8
*/
// PA10 is DTXD, which is the debug uart transmit pin
printf("Interrupt!!\n\r");
/* Check all pending interrupts */
// FIXME: this if condition is not always true...
// if ( (pinSmartCard.pio->PIO_ISR & pinSmartCard.mask) != 0 )
{
/* Check current level on pin */
if ( PIO_Get( &pinSmartCard ) == 0 )
{
sim_inserted = 1;
printf( "-I- Smartcard inserted\n\r" ) ;
CCID_Insertion();
}
else
{
sim_inserted = 0;
printf( "-I- Smartcard removed\n\r" ) ;
CCID_Removal();
}
}
}
/**
* Configures the smartcard detection pin to trigger an interrupt.
*/
static void ConfigureCardDetection( void )
{
printf("+++++ Configure PIOs\n\r");
PIO_Configure( &pinSmartCard, 1 ) ;
NVIC_EnableIRQ( PIOA_IRQn );
// FIXME: Do we need to set priority?: NVIC_SetPriority( PIOA_IRQn, 10);
PIO_ConfigureIt( &pinSmartCard, ISR_PioSmartCard ) ;
PIO_EnableIt( &pinSmartCard ) ;
}
#else
/**
* Dummy implementation.
*/
static void ConfigureCardDetection( void )
{
printf( "-I- Smartcard detection not supported.\n\r" ) ;
}
#endif
/*-----------------------------------------------------------------------------
* Initialization and run
*-----------------------------------------------------------------------------*/
static const CCIDDriverConfigurationDescriptors *configDescCCID;
extern CCIDDriverConfigurationDescriptors configurationDescriptorCCID;
void CCID_init( void )
{
uint8_t pAtr[MAX_ATR_SIZE];
uint8_t ucSize ;
configDescCCID = &configurationDescriptorCCID;
// FIXME: do we want to print ATR?
/* Initialize Atr buffer */
memset( pAtr, 0, sizeof( pAtr ) ) ;
/* Configure IT on Smart Card */
ConfigureCardDetection() ;
// Configure ISO7816 driver
PIO_Configure(pinsISO7816, PIO_LISTSIZE(pinsISO7816));
PIO_Configure(pinsBus, PIO_LISTSIZE(pinsBus));
PIO_Configure(pinsPower, PIO_LISTSIZE(pinsPower));
/* power up the card */
// PIO_Set(&pinsPower[0]);
ISO7816_Init( &pinIso7816RstMC ) ;
CCIDDriver_Initialize();
/* Read ATR */
ISO7816_warm_reset() ;
ISO7816_Datablock_ATR( pAtr, &ucSize ) ;
/* Decode ATR and print it */
ISO7816_Decode_ATR( pAtr ) ;
// FIXME. what if smcard is not inserted?
if(PIO_Get(&pinSmartCard) == 0) {
printf("SIM card inserted\n\r");
CCID_Insertion();
}
}
void CCID_run( void )
{
//if (USBD_Read(INT, pBuffer, dLength, fCallback, pArgument);
CCID_SmartCardRequest();
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,382 @@
/* ----------------------------------------------------------------------------
* ATMEL Microcontroller Software Support
* ----------------------------------------------------------------------------
* Copyright (c) 2008, Atmel Corporation
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Atmel's name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ----------------------------------------------------------------------------
*/
//------------------------------------------------------------------------------
/// \unit
///
/// !Purpose
///
/// Definition of methods for using a CCID device driver.
///
/// !Usage
///
/// -# CCIDDriver_Initialize
/// -# CCID_Read
/// -# CCID_Write
/// -# CCID_SmartCardRequest
/// -# CCID_Insertion
/// -# CCID_Removal
/// -# RDRtoPCHardwareError
//------------------------------------------------------------------------------
#ifndef CCID_DRIVER_H
#define CCID_DRIVER_H
#include "USBD.h"
/// For reference, the absolute maximum block size
/// for a TPDU T=0 block is 260 bytes (5 bytes command; 255 bytes data), or
/// for a TPDU T=1 block is 259 bytes, or
/// for a short APDU T=1 block is 261 bytes, or
/// for an extended APDU T=1 block is 65544 bytes.
#define ABDATA_SIZE 260
/// define protocol T=0
#define PROTOCOL_TO 0
/// define protocol T=1
#define PROTOCOL_T1 1
/// define for dwFeatures see Table 5.1-1 Smart Card Device Class Descriptors
/// No special characteristics
#define CCID_FEATURES_NADA 0x00000000
/// Automatic parameter configuration based on ATR data
#define CCID_FEATURES_AUTO_PCONF 0x00000002
/// Automatic activation of ICC on inserting
#define CCID_FEATURES_AUTO_ACTIV 0x00000004
/// Automatic ICC voltage selection
#define CCID_FEATURES_AUTO_VOLT 0x00000008
/// Automatic ICC clock frequency change according to active parameters provided
/// by the Host or self determined
#define CCID_FEATURES_AUTO_CLOCK 0x00000010
/// Automatic baud rate change according to active parameters provided by the
/// Host or self determined
#define CCID_FEATURES_AUTO_BAUD 0x00000020
/// Automatic parameters negotiation made by the CCID (use of warm or cold
/// resets or PPS according to a manufacturer proprietary algorithm to select
/// the communication parameters with the ICC)
#define CCID_FEATURES_AUTO_PNEGO 0x00000040
/// Automatic PPS made by the CCID according to the active parameters
#define CCID_FEATURES_AUTO_PPS 0x00000080
/// CCID can set ICC in clock stop mode
#define CCID_FEATURES_ICCSTOP 0x00000100
/// NAD value other than 00 accepted (T=1 protocol in use)
#define CCID_FEATURES_NAD 0x00000200
/// Automatic IFSD exchange as first exchange (T=1 protocol in use)
#define CCID_FEATURES_AUTO_IFSD 0x00000400
/// TPDU level exchanges with CCID
#define CCID_FEATURES_EXC_TPDU 0x00010000
/// Short APDU level exchange with CCID
#define CCID_FEATURES_EXC_SAPDU 0x00020000
/// Short and Extended APDU level exchange with CCID
#define CCID_FEATURES_EXC_APDU 0x00040000
/// USB Wake up signaling supported on card insertion and removal
#define CCID_FEATURES_WAKEUP 0x00100000
//------------------------------------------------------------------------------
// Types
//------------------------------------------------------------------------------
/// Bulk CCID Message header structure
typedef struct
{
unsigned char bMessageType;
/// Message-specific data length
unsigned long wLength;
/// Identifies the slot number for this command
unsigned char bSlot;
/// Sequence number for command.
unsigned char bSeq;
/// Slot status register
unsigned char bStatus;
/// Slot error
unsigned char bError;
/// specific register
unsigned char bSpecific;
/// Data block sent to the CCID.
unsigned char abData[ABDATA_SIZE];
unsigned char bSizeToSend;
} __attribute__ ((packed)) S_ccid_bulk_in_header;
/// 6.1 Bulk Transfers
typedef struct
{
unsigned char bMessageType;
/// Message-specific data length
unsigned long wLength;
/// Identifies the slot number for this command
unsigned char bSlot;
/// Sequence number for command.
unsigned char bSeq;
/// specific register
unsigned char bSpecific_0;
unsigned char bSpecific_1;
unsigned char bSpecific_2;
/// Application Protocol Data Unit
unsigned char APDU[ABDATA_SIZE];
} __attribute__ ((packed)) S_ccid_bulk_out_header;
/// 6.1.11.2 PIN Verification Data Structure
typedef struct
{
/// Number of seconds.
unsigned char bTimerOut;
/// Several parameters for the PIN format options
unsigned char bmFormatString;
/// Define the length of the PIN to present in the APDU command
unsigned char bmPINBlockString;
/// Allows the length PIN insertion in the APDU command
unsigned char bmPINLengthFormat;
/// Minimum PIN size in digit and Maximum PIN size in digit
unsigned char wPINMaxExtraDigit;
/// The value is a bit wise OR operation.
unsigned char bEntryValidationCondition;
/// Number of messages to display for the PIN modify command
unsigned char bNumberMessage;
/// Language used to display the messages.
unsigned char wLangId;
/// Message index in the Reader message table
unsigned char bMsgIndex;
/// T=1 I-block prologue field to use
unsigned char bTeoPrologue[3];
/// APDU to send to the ICC
unsigned char abPINApdu[255];
}__attribute__ ((packed)) S_ccid_PIN_Verification;
/// 6.1.11.7 PIN Modification Data Structure
typedef struct
{
/// Number of seconds. If 00h then CCID default value is used.
unsigned char bTimeOut;
/// Several parameters for the PIN format options (defined in § 6.1.11.4)
unsigned char bmFormatString4;
/// Define the length of the PIN to present in the APDU command
unsigned char bmPINBlockString;
/// Allows the length PIN insertion in the APDU command (defined in § 6.1.11.6)
unsigned char bmPinLengthFormat;
/// Insertion position offset in byte for the current PIN
unsigned char bInsertionOffsetOld;
/// Insertion position offset in byte for the new PIN
unsigned char bInsertionOffsetNew;
/// XXYYh
/// XX: Minimum PIN size in digit
/// YY: Maximum PIN size in digit
unsigned char wPINMaxExtraDigit;
/// 00h,01h,02h,03h
/// Indicates if a confirmation is requested before acceptance of a new PIN (meaning that the user has to enter this new PIN twice before it is accepted)
/// Indicates if the current PIN must be entered and set in the same APDU field of not.
unsigned char bConfirmPIN;
/// The value is a bit wise OR operation.
/// 01h Max size reached
/// 02h Validation key pressed
/// 04h Timeout occurred
unsigned char bEntryValidationCondition;
/// 00h,01h,02h,03h,or FFh
/// Number of messages to display for the PIN modify command.
unsigned char bNumberMessage;
/// Language used to display the messages. The 16 bit
unsigned char wLangId;
/// Message index in the Reader message table (should be 00h or 01h).
unsigned char bMsgIndex1;
/// Message index in the Reader message table (should be 01h or 02h).
unsigned char bMsgIndex2;
/// Message index in the Reader message table (should be 02h).
unsigned char bMsgIndex3;
/// T=1 I-block prologue field to use. Significant only if protocol in use is T=1.
unsigned char bTeoPrologue[3];
/// Byte array APDU to send to the ICC
unsigned char abPINApdu[255];
}__attribute__ ((packed)) S_ccid_PIN_Modification;
/// Protocol Data Structure for Protocol T=0 (bProtocolNum=0, dwLength=00000005h)
typedef struct
{
/// B7-4 FI Index into the table 7 in ISO/IEC 7816-3:1997 selecting a
/// clock rate conversion factor
/// B3-0 DI - Index into the table 8 in ISO/IEC 7816-3:1997 selecting a
/// baud rate conversion factor
unsigned char bmFindexDindex;
/// For T=0 ,B0 0b, B7-2 000000b
/// B1 Convention used (b1=0 for direct, b1=1 for inverse)
unsigned char bmTCCKST0; // 0 to 2
/// Extra Guardtime between two characters. Add 0 to 254 etu to the normal
/// guardtime of 12etu. FFh is the same as 00h.
unsigned char bGuardTimeT0; // 0 to FF
/// WI for T=0 used to define WWT
unsigned char bWaitingIntegerT0; // 0 to FF
/// ICC Clock Stop Support
/// 00 = Stopping the Clock is not allowed
/// 01 = Stop with Clock signal Low
/// 02 = Stop with Clock signal High
/// 03 = Stop with Clock either High or Low
unsigned char bClockStop; // 0 to 3
} __attribute__ ((packed)) S_ccid_protocol_t0;
/// Protocol Data Structure for Protocol T=1 (bProtocolNum=1, dwLength=00000007h)
typedef struct
{
/// B7-4 FI Index into the table 7 in ISO/IEC 7816-3:1997 selecting a
/// clock rate conversion factor
/// B3-0 DI - Index into the table 8 in ISO/IEC 7816-3:1997 selecting a
/// baud rate conversion factor
unsigned char bmFindexDindex;
/// For T=1, B7-2 000100b
/// B0 Checksum type (b0=0 for LRC, b0=1 for CRC
/// B1 Convention used (b1=0 for direct, b1=1 for inverse)
unsigned char bmTCCKST1; // 10h, 11h, 12h, 13h
/// Extra Guardtime (0 to 254 etu between two characters).
/// If value is FFh, then guardtime is reduced by 1.
unsigned char bGuardTimeT1; // 0 to FF
/// B7-4 = BWI
/// B3-0 = CWI
unsigned char bmWaitingIntegersT1; // 0 to 9
/// ICC Clock Stop Support
/// 00 = Stopping the Clock is not allowed
/// 01 = Stop with Clock signal Low
/// 02 = Stop with Clock signal High
/// 03 = Stop with Clock either High or Low
unsigned char bClockStop; // 0 to 3
/// Size of negotiated IFSC
unsigned char bIFSC; // 0 to FE
/// Nad value used by CCID
unsigned char bNadValue; // 0 to FF
} __attribute__ ((packed)) S_ccid_protocol_t1;
/// Identifies the length of type of subordinate descriptors of a CCID device
/// Table 5.1-1 Smart Card Device Class descriptors
typedef struct
{
/// Size of this descriptor, in bytes.
unsigned char bLength;
/// Functional Descriptor type
unsigned char bDescriptorType;
/// Integrated Circuit(s) Cards Interface Devices (CCID) Specification
/// Release Number
unsigned short bcdCCID;
/// Index of the highest available slot. An USB-ICC is regarded as a single
/// slot CCID.
unsigned char bMaxSlotIndex;
/// This value indicates what voltages the CCID can supply to its slots.
/// It is a bitwise OR operation performed on the following values:
/// - 01h 5.0V
/// - 02h 3.0V
/// - 04h 1.8V
/// Other bits are RFU.
unsigned char bVoltageSupport;
/// RRRR Upper Word- is RFU = 0000h
/// PPPP Lower Word- Encodes the supported protocol types. A 1 in a given
/// bit position indicates support for the associated ISO protocol.
/// 0001h = Protocol T=0
/// 0002h = Protocol T=1
/// All other bits are reserved and must be set to zero. The field is
/// intended to correspond to the PCSC specification definitions.
unsigned long dwProtocols;
/// Default ICC clock frequency in KHz. This is an integer value.
unsigned long dwDefaultClock;
/// Maximum supported ICC clock frequency in KHz. This is an integer value.
unsigned long dwMaximumClock;
/// The number of clock frequencies that are supported by the CCID. If the
/// value is 00h, the supported clock frequencies are assumed to be the
/// default clock frequency defined by dwDefaultClock and the maximum clock
/// frequency defined by dwMaximumClock.
unsigned char bNumClockSupported;
/// Default ICC I/O data rate in bps. This is an integer value
unsigned long dwDataRate;
/// Maximum supported ICC I/O data rate in bps
unsigned long dwMaxDataRate;
/// The number of data rates that are supported by the CCID.
unsigned char bNumDataRatesSupported;
/// Indicates the maximum IFSD supported by CCID for protocol T=1.
unsigned long dwMaxIFSD;
/// - RRRR-Upper Word- is RFU = 0000h
/// - PPPP-Lower Word- encodes the supported protocol types. A 1 in a given
/// bit position indicates support for the associated protocol.
/// 0001h indicates support for the 2-wire protocol 1
/// 0002h indicates support for the 3-wire protocol 1
/// 0004h indicates support for the I2C protocol 1
/// All other values are outside of this specification, and must be handled
/// by vendor-supplied drivers.
unsigned long dwSynchProtocols;
/// The value is a bitwise OR operation performed on the following values:
/// - 00000000h No special characteristics
/// - 00000001h Card accept mechanism 2
/// - 00000002h Card ejection mechanism 2
/// - 00000004h Card capture mechanism 2
/// - 00000008h Card lock/unlock mechanism
unsigned long dwMechanical;
/// This value indicates what intelligent features the CCID has.
unsigned long dwFeatures;
/// For extended APDU level the value shall be between 261 + 10 (header) and
/// 65544 +10, otherwise the minimum value is the wMaxPacketSize of the
/// Bulk-OUT endpoint.
unsigned long dwMaxCCIDMessageLength;
/// Significant only for CCID that offers an APDU level for exchanges.
unsigned char bClassGetResponse;
/// Significant only for CCID that offers an extended APDU level for exchanges.
unsigned char bClassEnvelope;
/// Number of lines and characters for the LCD display used to send messages for PIN entry.
unsigned short wLcdLayout;
/// This value indicates what PIN support features the CCID has.
unsigned char bPINSupport;
/// Maximum number of slots which can be simultaneously busy.
unsigned char bMaxCCIDBusySlots;
} __attribute__ ((packed)) CCIDDescriptor;
//------------------------------------------------------------------------------
// Exported functions
//------------------------------------------------------------------------------
extern unsigned char RDRtoPCHardwareError( unsigned char bSlot,
unsigned char bSeq,
unsigned char bHardwareErrorCode );
/*
#if !defined(NOAUTOCALLBACK)
extern void USBDCallbacks_RequestReceived(const USBGenericRequest *request);
#endif
*/
extern void CCID_SmartCardRequest( void );
extern void CCIDDriver_Initialize( void );
extern unsigned char CCID_Read(void *pBuffer,
unsigned int dLength,
TransferCallback fCallback,
void *pArgument);
extern unsigned char CCID_Write(void *pBuffer,
unsigned int dLength,
TransferCallback fCallback,
void *pArgument);
extern unsigned char CCID_Insertion( void );
extern unsigned char CCID_Removal( void );
#endif //#ifndef CCID_DRIVER_H

View File

@@ -0,0 +1,153 @@
/* ----------------------------------------------------------------------------
* ATMEL Microcontroller Software Support
* ----------------------------------------------------------------------------
* Copyright (c) 2008, Atmel Corporation
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Atmel's name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ----------------------------------------------------------------------------
*/
//------------------------------------------------------------------------------
// Title: cciddriverdescriptors.h
//
// About: Purpose
// Definitions of the descriptors required by the ccid device driver.
// DWG_Smart-Card_CCID_Rev110.pdf
//------------------------------------------------------------------------------
#ifndef CCID_DRIVER_DESCRIPTORS_H
#define CCID_DRIVER_DESCRIPTORS_H
//------------------------------------------------------------------------------
// Definitions
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Constants: Endpoints
// CCID_EPT_DATA_OUT endpoint data out bulk 1
// CCID_EPT_DATA_IN endpoint data in bulk 2
// CCID_EPT_NOTIFICATION endpoint data interupt 3
//------------------------------------------------------------------------------
#define CCID_EPT_DATA_OUT 1
#define CCID_EPT_DATA_IN 2
#define CCID_EPT_NOTIFICATION 3
//------------------------------------------------------------------------------
// USB-ICC protocol
//------------------------------------------------------------------------------
// CCID specification version 1.10
#define CCID1_10 0x0110
#define SMART_CARD_DEVICE_CLASS 0x0B
// Smart Card Device Class Descriptor Type
#define CCID_DECRIPTOR_TYPE 0x21
// Table 5.3-1 Summary of CCID Class Specific Request
#define CCIDGenericRequest_ABORT 0x01
#define CCIDGenericRequest_GET_CLOCK_FREQUENCIES 0x02
#define CCIDGenericRequest_GET_DATA_RATES 0x03
// 6.1 Command Pipe, Bulk-OUT Messages
#define PC_TO_RDR_ICCPOWERON 0x62
#define PC_TO_RDR_ICCPOWEROFF 0x63
#define PC_TO_RDR_GETSLOTSTATUS 0x65
#define PC_TO_RDR_XFRBLOCK 0x6F
#define PC_TO_RDR_GETPARAMETERS 0x6C
#define PC_TO_RDR_RESETPARAMETERS 0x6D
#define PC_TO_RDR_SETPARAMETERS 0x61
#define PC_TO_RDR_ESCAPE 0x6B
#define PC_TO_RDR_ICCCLOCK 0x6E
#define PC_TO_RDR_T0APDU 0x6A
#define PC_TO_RDR_SECURE 0x69
#define PC_TO_RDR_MECHANICAL 0x71
#define PC_TO_RDR_ABORT 0x72
#define PC_TO_RDR_SETDATARATEANDCLOCKFREQUENCY 0x73
// 6.2 Response Pipe, Bulk-IN Messages
#define RDR_TO_PC_DATABLOCK 0x80
#define RDR_TO_PC_SLOTSTATUS 0x81
#define RDR_TO_PC_PARAMETERS 0x82
#define RDR_TO_PC_ESCAPE 0x83
#define RDR_TO_PC_DATARATEANDCLOCKFREQUENCY 0x84
// 6.3 Interrupt-IN Messages
#define RDR_TO_PC_NOTIFYSLOTCHANGE 0x50
#define RDR_TO_PC_HARDWAREERROR 0x51
// Table 6.2-2 Slot error register when bmCommandStatus = 1
#define CMD_ABORTED 0xFF
#define ICC_MUTE 0xFE
#define XFR_PARITY_ERROR 0xFD
#define XFR_OVERRUN 0xFC
#define HW_ERROR 0xFB
#define BAD_ATR_TS 0xF8
#define BAD_ATR_TCK 0xF7
#define ICC_PROTOCOL_NOT_SUPPORTED 0xF6
#define ICC_CLASS_NOT_SUPPORTED 0xF5
#define PROCEDURE_BYTE_CONFLICT 0xF4
#define DEACTIVATED_PROTOCOL 0xF3
#define BUSY_WITH_AUTO_SEQUENCE 0xF2
#define PIN_TIMEOUT 0xF0
#define PIN_CANCELLED 0xEF
#define CMD_SLOT_BUSY 0xE0
// User defined 0xC0 to 0x81
// Reserved for futur use 0x80
// not supported incorrect message parameter 0x7F to 0x01
// Command not supported 0x00
// CCID rev 1.1, p.27
#define VOLTS_AUTO 0x00
#define VOLTS_5_0 0x01
#define VOLTS_3_0 0x02
#define VOLTS_1_8 0x03
// 6.3.1 RDR_to_PC_NotifySlotChange
#define ICC_NOT_PRESENT 0x00
#define ICC_PRESENT 0x01
#define ICC_CHANGE 0x02
#define ICC_INSERTED_EVENT 0x01
//FIXME: #define ICC_INSERTED_EVENT ICC_PRESENT+ICC_CHANGE
// ICCD: Table 6.1-8 Bitmap for bStatus field
#define ICC_BS_PRESENT_ACTIVATED 0x00 // USB-ICC is present and activated
#define ICC_BS_PRESENT_NOTACTIVATED 0x01 // USB-ICC is present but not activated
#define ICC_BS_NOTPRESENT 0x02 // USB-ICC is virtually not present
#define ICC_BS_RFU 0x03 // RFU
#define ICC_CS_NO_ERROR (0x00<<6) // Processed without error
#define ICC_CS_FAILED (0x01<<6) // Failed, error condition given by bError
#define ICC_CS_TIME_EXT (0x02<<6) // Time extension is requested
#define ICC_CS_RFU (0x03<<6) // RFU
/*
#define NO_ERROR 0x00
#define NO_EXTRA_BYTES 0x00
#define CCID_FLAG_INITIAL_VALUE 0x05
#define CCID_EVENT_SIZE 0x02
#define STATUS_MASK 0x41
*/
//------------------------------------------------------------------------------
// Structures
//------------------------------------------------------------------------------
#endif //#ifndef CCID_DRIVER_DESCRIPTORS_H

View File

@@ -0,0 +1,88 @@
// FIXME: Copyright license here
/*------------------------------------------------------------------------------
* Headers
*------------------------------------------------------------------------------*/
#include "board.h"
/*------------------------------------------------------------------------------
* Internal variables
*------------------------------------------------------------------------------*/
typedef struct {
void (* init) ( void );
void (* run) ( void );
} conf_func;
conf_func config_func_ptrs[] = {
{Sniffer_Init, Sniffer_run}, /* CFG_NUM_SNIFF */
{CCID_init, CCID_run}, /* CFG_NUM_CCID */
{Phone_Master_Init, Phone_run}, /* CFG_NUM_PHONE */
{MITM_init, MITM_run}, /* CFG_NUM_MITM */
};
/*------------------------------------------------------------------------------
* Internal variables
*------------------------------------------------------------------------------*/
uint8_t simtrace_config = CFG_NUM_SNIFF;
uint8_t conf_changed = 1;
uint8_t rcvdChar = 0;
uint32_t char_stat = 0;
/*------------------------------------------------------------------------------
* Main
*------------------------------------------------------------------------------*/
extern int main( void )
{
uint8_t isUsbConnected = 0;
LED_Configure(LED_NUM_RED);
LED_Configure(LED_NUM_GREEN);
LED_Set(LED_NUM_GREEN);
/* Disable watchdog*/
WDT_Disable( WDT ) ;
PIO_InitializeInterrupts(0);
SIMtrace_USB_Initialize();
printf("%s", "USB init\n\r");
// FIXME: why don't we get any interrupts with this line?:
while(USBD_GetState() < USBD_STATE_CONFIGURED);
TRACE_DEBUG("%s", "Start\n\r");
printf("%s", "Start\n\r");
while(1) {
/* Device is not configured */
if (USBD_GetState() < USBD_STATE_CONFIGURED) {
if (isUsbConnected) {
isUsbConnected = 0;
// TC_Stop(TC0, 0);
}
}
else if (isUsbConnected == 0) {
printf("USB is now configured\n\r");
isUsbConnected = 1;
// TC_Start(TC0, 0);
}
// for (int i=0; i <10000; i++);
/* FIXME: Or should we move the while loop into every case, and break out
in case the config changes? */
if (conf_changed) {
config_func_ptrs[simtrace_config-1].init();
conf_changed = 0;
} else {
config_func_ptrs[simtrace_config-1].run();
}
}
}

View File

@@ -0,0 +1,70 @@
/* ----------------------------------------------------------------------------
* ATMEL Microcontroller Software Support
* ----------------------------------------------------------------------------
* Copyright (c) 2009, Atmel Corporation
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Atmel's name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ----------------------------------------------------------------------------
*/
/*------------------------------------------------------------------------------
* Headers
*------------------------------------------------------------------------------*/
#include "board.h"
#include <string.h>
static const Pin pins_bus[] = {PINS_BUS_DEFAULT};
void MITM_init( void )
{
CCID_init();
Phone_Master_Init();
return;
/* Configure ISO7816 driver */
// FIXME: Phone and SIM card side:
// PIO_Configure( pinsISO7816_sniff, PIO_LISTSIZE( pinsISO7816_sniff ) ) ;
/*
PIO_Configure( pins_bus, PIO_LISTSIZE( pins_bus) ) ;
PIO_Configure(pPwr, PIO_LISTSIZE( pPwr ));
// FIXME: phone is clk master, sim is clk slave
_ISO7816_Init();
USART_SetReceiverEnabled(USART_SIM, 1);
USART_SetTransmitterEnabled(USART_SIM, 1);
USART_SetReceiverEnabled(USART_PHONE, 1);
USART_SetTransmitterEnabled(USART_PHONE, 1);
*/
}
void MITM_run( void )
{
Phone_run();
CCID_SmartCardRequest();
}

View File

@@ -0,0 +1,424 @@
/* ----------------------------------------------------------------------------
* ATMEL Microcontroller Software Support
* ----------------------------------------------------------------------------
* Copyright (c) 2009, Atmel Corporation
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Atmel's name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ----------------------------------------------------------------------------
*/
/*------------------------------------------------------------------------------
* Headers
*------------------------------------------------------------------------------*/
#include "board.h"
#include <string.h>
/*------------------------------------------------------------------------------
* Internal definitions
*------------------------------------------------------------------------------*/
/** Maximum ucSize in bytes of the smartcard answer to a command.*/
#define MAX_ANSWER_SIZE 10
/** Maximum ATR ucSize in bytes.*/
#define MAX_ATR_SIZE 55
/** USB states */
/// Use for power management
#define STATE_IDLE 0
/// The USB device is in suspend state
#define STATE_SUSPEND 4
/// The USB device is in resume state
#define STATE_RESUME 5
/* WTX (Wait time extension):
* R-block PCB begins with (msb) 10 , ends with 000011 for WTX req, 100011 for WTX resp
*
* The standard says:
* Rule 3 — If the card requires more than BWT to process the previously received I-block, it transmits S(WTX
* request) where INF conveys one byte encoding an integer multiplier of the BWT value. The interface device
* shall acknowledge by S(WTX response) with the same INF.
* The time allocated starts at the leading edge of the last character of S(WTX response).
*/
// FIXME: Two times the same name for the define, which one is right?
//#define WTX_req 0b10000011
//#define WTX_req 0b10100011
// Alternatively:
/* For T = 0 Protocol: The firmware on receiving the NULL (0x60) Procedure byte from the card, notifies
it to the driver using the RDR_to_PC_DataBlock response. During this period, the reception of bytes
from the smart card is still in progress and hence the device cannot indefinitely wait for IN tokens on
the USB bulk-in endpoint. Hence, it is required of the driver to readily supply IN tokens on the USB
bulk-in endpoint. On failure to do so, some of the wait time extension responses, will not be queued to
the driver.
*/
/*------------------------------------------------------------------------------
* Internal variables
*------------------------------------------------------------------------------*/
/** USB state: suspend, resume, idle */
unsigned char USBState = STATE_IDLE;
/** ISO7816 pins */
static const Pin pinsISO7816_PHONE[] = {PINS_ISO7816_PHONE};
/** Bus switch pins */
static const Pin pins_bus[] = {PINS_BUS_DEFAULT};
// FIXME: temporary enable bus switch
//static const Pin pins_bus[] = {PINS_BUS_SNIFF};
/** ISO7816 RST pin */
static const Pin pinIso7816RstMC = PIN_ISO7816_RST_PHONE;
static uint8_t sim_inserted = 0;
static const Pin pPwr[] = {
/* Enable power converter 4.5-6V to 3.3V; low: off */
{SIM_PWEN, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT},
/* Enable second power converter: VCC_PHONE to VCC_SIM; high: off */
{VCC_FWD, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_DEFAULT}
};
static const Pin pinPhoneRST = PIN_ISO7816_RST_PHONE;
#define PR TRACE_INFO
/* ===================================================*/
/* Taken from iso7816_4.c */
/* ===================================================*/
/** Flip flop for send and receive char */
#define USART_SEND 0
#define USART_RCV 1
enum states{
WAIT_FOR_RST = 9,
RST_RCVD = 10,
WAIT_CMD_PHONE = 11,
WAIT_CMD_PC = 12,
WAIT_ATR = 13,
};
/*-----------------------------------------------------------------------------
* Internal variables
*-----------------------------------------------------------------------------*/
/** Variable for state of send and receive froom USART */
static uint8_t StateUsartGlobal = USART_RCV;
static enum states state;
extern uint8_t rcvdChar;
extern volatile uint8_t timeout_occured;
/*-----------------------------------------------------------------------------
* Interrupt routines
*-----------------------------------------------------------------------------*/
#define RESET 'R'
static void ISR_PhoneRST( const Pin *pPin)
{
printf("+++ Int!! %x\n\r", pinPhoneRST.pio->PIO_ISR);
if ( ((pinPhoneRST.pio->PIO_ISR & pinPhoneRST.mask) != 0) )
{
if(PIO_Get( &pinPhoneRST ) == 0) {
printf(" 0 ");
} else {
printf(" 1 ");
}
}
state = RST_RCVD;
// FIXME: What to do on reset?
// FIXME: It seems like the phone is constantly sending a lot of these RSTs
PIO_DisableIt( &pinPhoneRST ) ;
}
static void Config_PhoneRST_IrqHandler()
{
PIO_Configure( &pinPhoneRST, 1);
// PIO_Configure( &pinPhoneClk, 1);
PIO_ConfigureIt( &pinPhoneRST, ISR_PhoneRST ) ;
// PIO_ConfigureIt( &pinPhoneClk, ISR_PhoneRST ) ;
PIO_EnableIt( &pinPhoneRST ) ;
// PIO_EnableIt( &pinPhoneClk ) ;
NVIC_EnableIRQ( PIOA_IRQn );
}
/**
* Get a character from ISO7816
* \param pCharToReceive Pointer for store the received char
* \return 0: if timeout else status of US_CSR
*/
/* FIXME: This code is taken from cciddriver.c
--> Reuse the code!!! */
uint32_t _ISO7816_GetChar( uint8_t *pCharToReceive )
{
uint32_t status;
uint32_t timeout=0;
TRACE_DEBUG("--");
if( StateUsartGlobal == USART_SEND ) {
while((USART_PHONE->US_CSR & US_CSR_TXEMPTY) == 0) {}
USART_PHONE->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
StateUsartGlobal = USART_RCV;
}
/* Wait USART ready for reception */
while( ((USART_PHONE->US_CSR & US_CSR_RXRDY) == 0) ) {
if(timeout++ > 12000 * (BOARD_MCK/1000000)) {
TRACE_DEBUG("TimeOut\n\r");
return( 0 );
}
}
/* At least one complete character has been received and US_RHR has not yet been read. */
/* Get a char */
*pCharToReceive = ((USART_PHONE->US_RHR) & 0xFF);
status = (USART_PHONE->US_CSR&(US_CSR_OVRE|US_CSR_FRAME|
US_CSR_PARE|US_CSR_TIMEOUT|US_CSR_NACK|
(1<<10)));
if (status != 0 ) {
TRACE_DEBUG("R:0x%X\n\r", status);
TRACE_DEBUG("R:0x%X\n\r", USART_PHONE->US_CSR);
TRACE_DEBUG("Nb:0x%X\n\r", USART_PHONE->US_NER );
USART_PHONE->US_CR = US_CR_RSTSTA;
}
/* Return status */
return( status );
}
/**
* Send a char to ISO7816
* \param CharToSend char to be send
* \return status of US_CSR
*/
uint32_t _ISO7816_SendChar( uint8_t CharToSend )
{
uint32_t status;
TRACE_DEBUG("********** Send char: %c (0x%X)\n\r", CharToSend, CharToSend);
if( StateUsartGlobal == USART_RCV ) {
USART_PHONE->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
StateUsartGlobal = USART_SEND;
}
/* Wait USART ready for transmit */
while((USART_PHONE->US_CSR & US_CSR_TXRDY) == 0) {}
/* There is no character in the US_THR */
/* Transmit a char */
USART_PHONE->US_THR = CharToSend;
status = (USART_PHONE->US_CSR&(US_CSR_OVRE|US_CSR_FRAME|
US_CSR_PARE|US_CSR_TIMEOUT|US_CSR_NACK|
(1<<10)));
if (status != 0 ) {
TRACE_DEBUG("******* status: 0x%X (Overrun: %lu, NACK: %lu, Timeout: %lu, underrun: %lu)\n\r",
status, ((status & US_CSR_OVRE)>> 5), ((status & US_CSR_NACK) >> 13),
((status & US_CSR_TIMEOUT) >> 8), ((status & (1 << 10)) >> 10));
TRACE_DEBUG("E (USART CSR reg):0x%X\n\r", USART_PHONE->US_CSR);
TRACE_DEBUG("Nb (Number of errors):0x%X\n\r", USART_PHONE->US_NER );
USART_PHONE->US_CR = US_CR_RSTSTA;
}
/* Return status */
return( status );
}
void Phone_Master_Init( void ) {
PIO_Configure( pinsISO7816_PHONE, PIO_LISTSIZE( pinsISO7816_PHONE ) ) ;
PIO_Configure( pins_bus, PIO_LISTSIZE( pins_bus) ) ;
Config_PhoneRST_IrqHandler();
_ISO7816_Init();
USART_SetTransmitterEnabled(USART_PHONE, 1);
USART_SetReceiverEnabled(USART_PHONE, 1);
/* Configure ISO7816 driver */
// FIXME: PIO_Configure(pPwr, PIO_LISTSIZE( pPwr ));
state = WAIT_FOR_RST;
// FIXME: Or do I need to call VBUS_CONFIGURE() here instead, which will call USBD_Connect() later?
// USBD_Connect();
// FIXME: USB clock? USB PMC?
// NVIC_EnableIRQ( UDP_IRQn );
USART_EnableIt( USART_PHONE, US_IER_RXRDY) ;
// FIXME: At some point USBD_IrqHandler() should get called and set USBD_STATE_CONFIGURED
/* while (USBD_GetState() < USBD_STATE_CONFIGURED) {
int i = 1;
if ((i%10000) == 0) {
TRACE_DEBUG("%d: USB State: %x\n\r", i, USBD_GetState());
}
i++;
}
*/
Timer_Init();
}
void send_ATR(uint8_t *ATR, uint8_t status, uint32_t transferred, uint32_t remaining)
{
uint32_t i;
if (status != USBD_STATUS_SUCCESS) {
TRACE_ERROR("USB err status (%s): %d", __FUNCTION__, status);
return;
}
PR("Send %x %x .. %x (tr: %d, st: %x)", ATR[0], ATR[1], ATR[transferred-1], transferred, status);
for ( i = 0; i < transferred; i++ ) {
_ISO7816_SendChar(*(ATR++));
}
state = WAIT_CMD_PHONE;
PIO_EnableIt( &pinPhoneRST ) ;
}
void sendResponse( uint8_t *pArg, uint8_t status, uint32_t transferred, uint32_t remaining)
{
uint32_t i;
if (status != USBD_STATUS_SUCCESS) {
TRACE_ERROR("USB err status (%s): %d", __FUNCTION__, status);
return;
}
PR("sendResp, stat: %X, trnsf: %x, rem: %x\n\r", status, transferred, remaining);
PR("Resp: %x %x %x .. %x", pArg[0], pArg[1], pArg[2], pArg[transferred-1]);
for ( i = 0; i < transferred; i++ ) {
_ISO7816_SendChar(*(pArg++));
}
/*
if (*(pArg-1) == 0x8A) {
for (i=0; i<20000; i++) ;
_ISO7816_SendChar(0x90);
_ISO7816_SendChar(0x00);
}
*/
state = WAIT_CMD_PHONE;
}
extern ring_buffer buf;
#define MAX_MSG_LEN 64
void wait_for_response(uint8_t pBuffer[]) {
int ret = 0;
// uint8_t msg[] = {0xa0, 0xa4, 0x0, 0x0, 0x2};
if (rcvdChar != 0) {
printf(" rr ");
/* DATA_IN for host side is data_out for simtrace side */
/* FIXME: Performancewise sending a USB packet for every byte is a disaster */
ret = USBD_Write( PHONE_DATAIN, buf.buf, BUFLEN, 0, 0 );
//USBD_Write( PHONE_DATAIN, msg, BUFLEN, 0, 0 );
PR("b:%x %x %x %x %x.\n\r", buf.buf[0], buf.buf[1],buf.buf[2], buf.buf[3], buf.buf[4]);
rcvdChar = 0;
} else if (timeout_occured && buf.idx != 0) {
printf(" to ");
ret = USBD_Write( PHONE_DATAIN, buf.buf, buf.idx, 0, 0 );
timeout_occured = 0;
buf.idx = 0;
rcvdChar = 0;
PR("b:%x %x %x %x %x.\n\r", buf.buf[0], buf.buf[1],buf.buf[2], buf.buf[3], buf.buf[4]);
} else {
return;
}
if ((ret = USBD_Read(PHONE_DATAOUT, pBuffer, MAX_MSG_LEN,
(TransferCallback)&sendResponse, pBuffer)) == USBD_STATUS_SUCCESS) {
PR("wait_rsp\n\r");
// state = WAIT_CMD_PC;
buf.idx = 0;
TC0_Counter_Reset();
} else {
PR("USB Err: %X", ret);
return;
}
}
// Sniffed Phone to SIM card communication:
// phone > sim : RST
// phone < sim : ATR
// phone > sim : A0 A4 00 00 02 (Select File)
// phone < sim : A4 (INS repeated)
// phone > sim : 7F 02 (= ??)
// phone < sim : 9F 16 (9F: success, can deliver 0x16 (=22) byte)
// phone > sim : ?? (A0 C0 00 00 16)
// phone < sim : C0 (INS repeated)
// phone < sim : 00 00 00 00 7F 20 02 00 00 00 00 00 09 91 00 17 04 00 83 8A (data of length 22 -2)
// phone <? sim : 90 00 (OK, everything went fine)
// phone ? sim : 00 (??)
void Phone_run( void )
{
int ret;
uint8_t pBuffer[MAX_MSG_LEN];
int msg = RESET;
// FIXME: remove:
// uint8_t ATR[] = {0x3B, 0x9A, 0x94, 0x00, 0x92, 0x02, 0x75, 0x93, 0x11, 0x00, 0x01, 0x02, 0x02, 0x19};
// send_ATR(ATR, (sizeof(ATR)/sizeof(ATR[0])));
switch (state) {
case RST_RCVD:
if ((ret = USBD_Write( PHONE_INT, &msg, 1, 0, 0 )) != USBD_STATUS_SUCCESS) {
PR("USB Error: %X", ret);
}
//buf.idx = 0;
//rcvdChar = 0;
// TC0_Counter_Reset();
// send_ATR sets state to WAIT_CMD
if ((ret = USBD_Read(PHONE_DATAOUT, pBuffer, MAX_MSG_LEN, (TransferCallback)&send_ATR, pBuffer)) == USBD_STATUS_SUCCESS) {
PR("Reading started sucessfully (ATR)");
state = WAIT_ATR;
} else {
// PR("USB Error: %X", ret);
//FIXME: state = ERR;
}
break;
case WAIT_CMD_PHONE:
// FIXME: TC0_Counter_Reset();
wait_for_response(pBuffer);
break;
case WAIT_FOR_RST:
break;
default:
// PR(":(");
break;
}
// FIXME: Function Phone_run not implemented yet
/* Send and receive chars */
// ISO7816_GetChar(&rcv_char);
// ISO7816_SendChar(char_to_send);
}

View File

@@ -0,0 +1,72 @@
#ifndef SIMTRACE_H
#define SIMTRACE_H
/* Endpoint numbers */
#define DATAOUT 1
#define DATAIN 2
#define INT 3
#define BUFLEN 5
#define PHONE_DATAOUT 4
#define PHONE_DATAIN 5
#define PHONE_INT 6
typedef struct ring_buffer
{
uint8_t buf[BUFLEN*2]; // data buffer
uint8_t idx; // number of items in the buffer
} ring_buffer;
enum confNum {
CFG_NUM_SNIFF = 1, CFG_NUM_CCID, CFG_NUM_PHONE, CFG_NUM_MITM, NUM_CONF
};
/// CCIDDriverConfiguration Descriptors
/// List of descriptors that make up the configuration descriptors of a
/// device using the CCID driver.
typedef struct {
/// Configuration descriptor
USBConfigurationDescriptor configuration;
/// Interface descriptor
USBInterfaceDescriptor interface;
/// CCID descriptor
CCIDDescriptor ccid;
/// Bulk OUT endpoint descriptor
USBEndpointDescriptor bulkOut;
/// Bulk IN endpoint descriptor
USBEndpointDescriptor bulkIn;
/// Interrupt OUT endpoint descriptor
USBEndpointDescriptor interruptIn;
} __attribute__ ((packed)) CCIDDriverConfigurationDescriptors;
extern const USBConfigurationDescriptor *configurationDescriptorsArr[];
/* Helper functions */
// FIXME: static function definitions
extern uint32_t _ISO7816_GetChar( uint8_t *pCharToReceive );
extern uint32_t _ISO7816_SendChar( uint8_t CharToSend );
/* Init functions */
extern void Phone_Master_Init( void );
extern void CCID_init( void );
extern void Sniffer_Init( void );
extern void MITM_init( void );
extern void SIMtrace_USB_Initialize( void );
extern void _ISO7816_Init( void );
/* Run functions */
extern void Sniffer_run( void );
extern void CCID_run( void );
extern void Phone_run( void );
extern void MITM_run( void );
/* Timer helper function */
void Timer_Init( void );
void TC0_Counter_Reset( void );
#endif /* SIMTRACE_H */

View File

@@ -0,0 +1,151 @@
/* ----------------------------------------------------------------------------
* ATMEL Microcontroller Software Support
* ----------------------------------------------------------------------------
* Copyright (c) 2009, Atmel Corporation
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Atmel's name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ----------------------------------------------------------------------------
*/
/*------------------------------------------------------------------------------
* Headers
*------------------------------------------------------------------------------*/
#include "board.h"
#include <string.h>
extern uint8_t rcvdChar;
extern uint32_t char_stat;
//#define BUFLEN 14
// FIXME: Remove:
#define PR TRACE_INFO
//#define PR printf
/*typedef struct ring_buffer
{
uint8_t buf[BUFLEN*2]; // data buffer
uint8_t idx; // number of items in the buffer
} ring_buffer;
*/
ring_buffer buf = { {0}, 0 };
void buf_push(uint8_t item)
{
buf.buf[buf.idx % (BUFLEN*2)] = item;
PR("Psh: %x %x\n\r", buf.idx, buf.buf[buf.idx]);
buf.idx = (buf.idx+1) % (BUFLEN*2);
}
/** Initializes a ISO driver
*/
// FIXME: This function is implemented in iso7816_4.c !! Only MCK instead of SCK is always taken. Change that!
void _ISO7816_Init( void )
{
printf("ISO_Init\n\r");
TRACE_DEBUG("ISO_Init\n\r");
USART_Configure( USART_PHONE,
US_MR_USART_MODE_IS07816_T_0
// Nope, we aren't master:
// | US_MR_USCLKS_MCK
| US_MR_USCLKS_SCK
| US_MR_NBSTOP_1_BIT
| US_MR_PAR_EVEN
| US_MR_CHRL_8_BIT
| US_MR_CLKO /** TODO: This field was set in the original simtrace firmware..why? */
| (3<<24), /* MAX_ITERATION */
1,
0);
/*
SYNC = 0 (async mode)
OVER = 0 (oversampling by 8?)
FIDI = 372 (default val on startup before other value is negotiated)
USCLKS = 3 (Select SCK as input clock) --> US_MR_USCLKS_SCK
CD = 1 ? --> US_BRGR_CD(1)
*/
USART_PHONE->US_FIDI = 372;
// USART_PHONE->US_IDR = (uint32_t) -1;
USART_PHONE->US_BRGR = US_BRGR_CD(1);
// USART_PHONE->US_BRGR = BOARD_MCK / (372*9600);
USART_PHONE->US_TTGR = 5;
/* Configure USART */
PMC_EnablePeripheral(ID_USART_PHONE);
USART1->US_IDR = 0xffffffff;
USART_EnableIt( USART1, US_IER_RXRDY) ;
/* enable USART1 interrupt */
NVIC_EnableIRQ( USART1_IRQn ) ;
// USART_PHONE->US_IER = US_IER_RXRDY | US_IER_OVRE | US_IER_FRAME | US_IER_PARE | US_IER_NACK | US_IER_ITER;
}
/*
* Initializes rcvdChar with the char received on USART interface
* char_stat is zero if no error occured.
* Otherwise it is filled with the content of the status register.
*/
void USART1_IrqHandler( void )
{
uint32_t stat;
char_stat = 0;
// Rcv buf full
/* if((stat & US_CSR_RXBUFF) == US_CSR_RXBUFF) {
TRACE_DEBUG("Rcv buf full");
USART_DisableIt(USART1, US_IDR_RXBUFF);
}
*/
uint32_t csr = USART_PHONE->US_CSR;
// PR("---- stat: %x\n\r", csr);
if (csr & US_CSR_TXRDY) {
/* transmit buffer empty, nothing to transmit */
}
if (csr & US_CSR_RXRDY) {
stat = (csr&(US_CSR_OVRE|US_CSR_FRAME|
US_CSR_PARE|US_CSR_TIMEOUT|US_CSR_NACK|
(1<<10)));
int c = (USART_PHONE->US_RHR) & 0xFF;
// printf(" %x", c);
if (stat == 0 ) {
/* Fill char into buffer */
buf_push((USART_PHONE->US_RHR) & 0xFF);
} else {
// buf_push((USART_PHONE->US_RHR) & 0xFF);
PR("e");
PR("%x\n\r", (USART_PHONE->US_RHR) & 0xFF);
PR("st: %x ", stat);
} /* else: error occured */
if ((buf.idx % BUFLEN) == BUFLEN-1) {
rcvdChar = 1;
printf("r. ");
}
char_stat = stat;
}
}

View File

@@ -0,0 +1,94 @@
/* ----------------------------------------------------------------------------
* ATMEL Microcontroller Software Support
* ----------------------------------------------------------------------------
* Copyright (c) 2009, Atmel Corporation
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Atmel's name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ----------------------------------------------------------------------------
*/
/*------------------------------------------------------------------------------
* Headers
*------------------------------------------------------------------------------*/
#include "board.h"
#include <string.h>
/*------------------------------------------------------------------------------
* Internal definitions
*------------------------------------------------------------------------------*/
// FIXME: Remove:
#define PR TRACE_DEBUG
/** Maximum ucSize in bytes of the smartcard answer to a command.*/
#define MAX_ANSWER_SIZE 10
/** Maximum ATR ucSize in bytes.*/
#define MAX_ATR_SIZE 55
/*------------------------------------------------------------------------------
* Internal variables
*------------------------------------------------------------------------------*/
/** ISO7816 pins */
static const Pin pinsISO7816_sniff[] = {PINS_SIM_SNIFF_SIM};
static const Pin pins_bus[] = {PINS_BUS_SNIFF};
static const Pin pPwr[] = {
/* Enable power converter 4.5-6V to 3.3V; low: off */
{SIM_PWEN, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT},
/* Enable second power converter: VCC_PHONE to VCC_SIM; high: on */
{VCC_FWD, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_DEFAULT}
};
extern uint32_t char_stat;
extern uint8_t rcvdChar;
extern ring_buffer buf;
/*-----------------------------------------------------------------------------
* Initialization routine
*-----------------------------------------------------------------------------*/
void Sniffer_Init( void )
{
/* Configure ISO7816 driver */
PIO_Configure( pinsISO7816_sniff, PIO_LISTSIZE( pinsISO7816_sniff ) ) ;
PIO_Configure( pins_bus, PIO_LISTSIZE( pins_bus) ) ;
PIO_Configure(pPwr, PIO_LISTSIZE( pPwr ));
_ISO7816_Init();
USART_SetReceiverEnabled(USART_PHONE, 1);
}
void Sniffer_run( void )
{
if (rcvdChar != 0) {
/* DATA_IN for host side is data_out for simtrace side */
/* FIXME: Performancewise sending a USB packet for every byte is a disaster */
PR("----- %x %x %x ..\n\r", buf.buf[0], buf.buf[1],buf.buf[2] );
USBD_Write( DATAIN, buf.buf, BUFLEN, 0, 0 );
PR("----- Rcvd char\n\r");
rcvdChar = 0;
}
}

View File

@@ -0,0 +1,116 @@
/* SimTrace TC (Timer / Clock) support code
* (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/*------------------------------------------------------------------------------
* Headers
*------------------------------------------------------------------------------*/
#include "board.h"
#include <string.h>
//FIXME:
static const Pin pTC[] = {{PIO_PA4B_TCLK0, PIO_PA0B_TIOA0, PIO_PA1B_TIOB0}};
/** Global timestamp in milliseconds since start of application */
volatile uint32_t dwTimeStamp = 0;
volatile uint8_t timeout_occured = 0;
// FIXME: Do I need the function?:
/**
* \brief Handler for Sytem Tick interrupt.
*
* Process System Tick Event
* Increments the timestamp counter.
*/
void SysTick_Handler( void )
{
dwTimeStamp ++;
}
void TC0_IrqHandler( void )
{
volatile uint32_t dummy;
/* Clear status bit to acknowledge interrupt */
dummy = TC0->TC_CHANNEL[ 0 ].TC_SR;
timeout_occured++;
// printf("++++ TC0_Irq %d\n\r", timeout_occured);
}
void TC0_Counter_Reset( void )
{
TC0->TC_CHANNEL[ 0 ].TC_CCR = TC_CCR_SWTRG ;
timeout_occured = 0;
}
/* == Timeouts ==
* One symbol is about 2ms --> Timeout = BUFLEN * 2ms ?
* For BUFLEN = 64 that is 7.8 Hz
*/
void Timer_Init()
{
uint32_t div;
uint32_t tcclks;
/** Enable peripheral clock. */
PMC_EnablePeripheral(ID_TC0);
/** Configure TC for a $ARG1 Hz frequency and trigger on RC compare. */
TC_FindMckDivisor( 8, BOARD_MCK, &div, &tcclks, BOARD_MCK );
TRACE_INFO("Chosen div, tcclk: %d, %d\r\n", div, tcclks);
/* TC_CMR: TC Channel Mode Register: Capture Mode */
/* CPCTRG: RC Compare resets the counter and starts the counter clock. */
TC_Configure( TC0, 0, tcclks | TC_CMR_CPCTRG );
/* TC_RC: TC Register C: contains the Register C value in real time. */
TC0->TC_CHANNEL[ 0 ].TC_RC = ( BOARD_MCK / div ) / 4;
/* Configure and enable interrupt on RC compare */
NVIC_EnableIRQ( (IRQn_Type)ID_TC0 );
TC0->TC_CHANNEL[ 0 ].TC_IER = TC_IER_CPCS; /* CPCS: RC Compare */
TC_Start( TC0, 0 );
return;
/*** From here on we have code based on old simtrace code */
/* Cfg PA4(TCLK0), PA0(TIOA0), PA1(TIOB0) */
PIO_Configure( pTC, PIO_LISTSIZE( pTC ) );
// FIXME:
// PIO_ConfigureIt( &pinPhoneRST, ISR_PhoneRST ) ;
// PIO_EnableIt( &pinPhoneRST ) ;
/* enable interrupts for Compare-C and External Trigger */
TC0->TC_CHANNEL[0].TC_IER = TC_IER_CPCS | TC_IER_ETRGS;
//...
/* Enable master clock for TC0 */
// TC0->TC_CHANNEL[0].TC_CCR
/* Reset to start timers */
//...
}

657
firmware/src_simtrace/usb.c Normal file
View File

@@ -0,0 +1,657 @@
/* ----------------------------------------------------------------------------
* ATMEL Microcontroller Software Support
* ----------------------------------------------------------------------------
* Copyright (c) 2009, Atmel Corporation
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Atmel's name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ----------------------------------------------------------------------------
*/
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
#include "board.h"
#include <cciddriverdescriptors.h>
/*------------------------------------------------------------------------------
* USB String descriptors
*------------------------------------------------------------------------------*/
static const unsigned char langDesc[] = {
USBStringDescriptor_LENGTH(1),
USBGenericDescriptor_STRING,
USBStringDescriptor_ENGLISH_US
};
const unsigned char productStringDescriptor[] = {
USBStringDescriptor_LENGTH(8),
USBGenericDescriptor_STRING,
USBStringDescriptor_UNICODE('S'),
USBStringDescriptor_UNICODE('I'),
USBStringDescriptor_UNICODE('M'),
USBStringDescriptor_UNICODE('t'),
USBStringDescriptor_UNICODE('r'),
USBStringDescriptor_UNICODE('a'),
USBStringDescriptor_UNICODE('c'),
USBStringDescriptor_UNICODE('e'),
};
const unsigned char snifferConfigStringDescriptor[] = {
USBStringDescriptor_LENGTH(15),
USBGenericDescriptor_STRING,
USBStringDescriptor_UNICODE('S'),
USBStringDescriptor_UNICODE('I'),
USBStringDescriptor_UNICODE('M'),
USBStringDescriptor_UNICODE('t'),
USBStringDescriptor_UNICODE('r'),
USBStringDescriptor_UNICODE('a'),
USBStringDescriptor_UNICODE('c'),
USBStringDescriptor_UNICODE('e'),
USBStringDescriptor_UNICODE('S'),
USBStringDescriptor_UNICODE('n'),
USBStringDescriptor_UNICODE('i'),
USBStringDescriptor_UNICODE('f'),
USBStringDescriptor_UNICODE('f'),
USBStringDescriptor_UNICODE('e'),
USBStringDescriptor_UNICODE('r'),
};
const unsigned char CCIDConfigStringDescriptor[] = {
USBStringDescriptor_LENGTH(12),
USBGenericDescriptor_STRING,
USBStringDescriptor_UNICODE('S'),
USBStringDescriptor_UNICODE('I'),
USBStringDescriptor_UNICODE('M'),
USBStringDescriptor_UNICODE('t'),
USBStringDescriptor_UNICODE('r'),
USBStringDescriptor_UNICODE('a'),
USBStringDescriptor_UNICODE('c'),
USBStringDescriptor_UNICODE('e'),
USBStringDescriptor_UNICODE('C'),
USBStringDescriptor_UNICODE('C'),
USBStringDescriptor_UNICODE('I'),
USBStringDescriptor_UNICODE('D'),
};
const unsigned char phoneConfigStringDescriptor[] = {
USBStringDescriptor_LENGTH(13),
USBGenericDescriptor_STRING,
USBStringDescriptor_UNICODE('S'),
USBStringDescriptor_UNICODE('I'),
USBStringDescriptor_UNICODE('M'),
USBStringDescriptor_UNICODE('t'),
USBStringDescriptor_UNICODE('r'),
USBStringDescriptor_UNICODE('a'),
USBStringDescriptor_UNICODE('c'),
USBStringDescriptor_UNICODE('e'),
USBStringDescriptor_UNICODE('P'),
USBStringDescriptor_UNICODE('h'),
USBStringDescriptor_UNICODE('o'),
USBStringDescriptor_UNICODE('n'),
USBStringDescriptor_UNICODE('e'),
};
const unsigned char MITMConfigStringDescriptor[] = {
USBStringDescriptor_LENGTH(12),
USBGenericDescriptor_STRING,
USBStringDescriptor_UNICODE('S'),
USBStringDescriptor_UNICODE('I'),
USBStringDescriptor_UNICODE('M'),
USBStringDescriptor_UNICODE('t'),
USBStringDescriptor_UNICODE('r'),
USBStringDescriptor_UNICODE('a'),
USBStringDescriptor_UNICODE('c'),
USBStringDescriptor_UNICODE('e'),
USBStringDescriptor_UNICODE('M'),
USBStringDescriptor_UNICODE('I'),
USBStringDescriptor_UNICODE('T'),
USBStringDescriptor_UNICODE('M'),
};
enum strDescNum {
PRODUCT_STRING = 1, SNIFFER_CONF_STR, CCID_CONF_STR, PHONE_CONF_STR, MITM_CONF_STR, STRING_DESC_CNT
};
/** List of string descriptors used by the device */
const unsigned char *stringDescriptors[] = {
/* FIXME: Is it true that I can't use the string desc #0,
* because 0 also stands for "no string desc"?
* on the other hand, dmesg output:
* "string descriptor 0 malformed (err = -61), defaulting to 0x0409" */
langDesc,
productStringDescriptor,
snifferConfigStringDescriptor,
CCIDConfigStringDescriptor,
phoneConfigStringDescriptor,
MITMConfigStringDescriptor
};
/*------------------------------------------------------------------------------
* USB Device descriptors
*------------------------------------------------------------------------------*/
typedef struct _SIMTraceDriverConfigurationDescriptorSniffer {
/** Standard configuration descriptor. */
USBConfigurationDescriptor configuration;
USBInterfaceDescriptor sniffer;
USBEndpointDescriptor sniffer_dataOut;
USBEndpointDescriptor sniffer_dataIn;
USBEndpointDescriptor sniffer_interruptIn;
} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorSniffer;
const SIMTraceDriverConfigurationDescriptorSniffer configurationDescriptorSniffer = {
/* Standard configuration descriptor */
{
sizeof(USBConfigurationDescriptor),
USBGenericDescriptor_CONFIGURATION,
sizeof(SIMTraceDriverConfigurationDescriptorSniffer),
1, /* There is one interface in this configuration */
CFG_NUM_SNIFF, /* configuration number */
SNIFFER_CONF_STR, /* string descriptor for this configuration */
USBD_BMATTRIBUTES,
USBConfigurationDescriptor_POWER(100)
},
/* Communication class interface standard descriptor */
{
sizeof(USBInterfaceDescriptor),
USBGenericDescriptor_INTERFACE,
0, /* This is interface #0 */
0, /* This is alternate setting #0 for this interface */
3, /* Number of endpoints */
0xff, /* Descriptor Class: Vendor specific */
0, /* No subclass */
0, /* No l */
SNIFFER_CONF_STR /* Third in string descriptor for this interface */
},
/* Bulk-OUT endpoint standard descriptor */
{
sizeof(USBEndpointDescriptor),
USBGenericDescriptor_ENDPOINT,
USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
DATAOUT),
USBEndpointDescriptor_BULK,
MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAOUT),
USBEndpointDescriptor_MAXBULKSIZE_FS),
0 /* Must be 0 for full-speed bulk endpoints */
},
/* Bulk-IN endpoint descriptor */
{
sizeof(USBEndpointDescriptor),
USBGenericDescriptor_ENDPOINT,
USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
DATAIN),
USBEndpointDescriptor_BULK,
MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAIN),
USBEndpointDescriptor_MAXBULKSIZE_FS),
0 /* Must be 0 for full-speed bulk endpoints */
},
// Notification endpoint descriptor
{
sizeof(USBEndpointDescriptor),
USBGenericDescriptor_ENDPOINT,
USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, INT ),
USBEndpointDescriptor_INTERRUPT,
MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(INT),
USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
0x10
}
};
/*
/// CCIDDriverConfiguration Descriptors
/// List of descriptors that make up the configuration descriptors of a
/// device using the CCID driver.
typedef struct {
/// Configuration descriptor
USBConfigurationDescriptor configuration;
/// Interface descriptor
USBInterfaceDescriptor interface;
/// CCID descriptor
CCIDDescriptor ccid;
/// Bulk OUT endpoint descriptor
USBEndpointDescriptor bulkOut;
/// Bulk IN endpoint descriptor
USBEndpointDescriptor bulkIn;
/// Interrupt OUT endpoint descriptor
USBEndpointDescriptor interruptIn;
} __attribute__ ((packed)) CCIDDriverConfigurationDescriptors;
*/
const CCIDDriverConfigurationDescriptors configurationDescriptorCCID = {
// Standard USB configuration descriptor
{
sizeof(USBConfigurationDescriptor),
USBGenericDescriptor_CONFIGURATION,
sizeof(CCIDDriverConfigurationDescriptors),
1, // One interface in this configuration
CFG_NUM_CCID, // This is configuration #1
CCID_CONF_STR, // associated string descriptor
BOARD_USB_BMATTRIBUTES,
USBConfigurationDescriptor_POWER(100)
},
// CCID interface descriptor
// Table 4.3-1 Interface Descriptor
// Interface descriptor
{
sizeof(USBInterfaceDescriptor),
USBGenericDescriptor_INTERFACE,
0, // Interface 0
0, // No alternate settings
3, // uses bulk-IN, bulk-OUT and interrupt IN
SMART_CARD_DEVICE_CLASS,
0, // Subclass code
0, // bulk transfers optional interrupt-IN
CCID_CONF_STR // associated string descriptor
},
{
sizeof(CCIDDescriptor), // bLength: Size of this descriptor in bytes
CCID_DECRIPTOR_TYPE, // bDescriptorType:Functional descriptor type
CCID1_10, // bcdCCID: CCID version
0, // bMaxSlotIndex: Value 0 indicates that one slot is supported
VOLTS_5_0, // bVoltageSupport
(1 << PROTOCOL_TO), // dwProtocols
3580, // dwDefaultClock
3580, // dwMaxClock
0, // bNumClockSupported
9600, // dwDataRate : 9600 bauds
9600, // dwMaxDataRate : 9600 bauds
0, // bNumDataRatesSupported
0xfe, // dwMaxIFSD
0, // dwSynchProtocols
0, // dwMechanical
//0x00010042, // dwFeatures: Short APDU level exchanges
CCID_FEATURES_AUTO_CLOCK | CCID_FEATURES_AUTO_BAUD |
CCID_FEATURES_AUTO_PCONF | CCID_FEATURES_AUTO_PNEGO | CCID_FEATURES_EXC_TPDU,
0x0000010F, // dwMaxCCIDMessageLength: For extended APDU level the value shall be between 261 + 10
0xFF, // bClassGetResponse: Echoes the class of the APDU
0xFF, // bClassEnvelope: Echoes the class of the APDU
0, // wLcdLayout: no LCD
0, // bPINSupport: No PIN
1 // bMaxCCIDBusySlot
},
// Bulk-OUT endpoint descriptor
{
sizeof(USBEndpointDescriptor),
USBGenericDescriptor_ENDPOINT,
USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_OUT, CCID_EPT_DATA_OUT ),
USBEndpointDescriptor_BULK,
MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_DATA_OUT),
USBEndpointDescriptor_MAXBULKSIZE_FS),
0x00 // Does not apply to Bulk endpoints
},
// Bulk-IN endpoint descriptor
{
sizeof(USBEndpointDescriptor),
USBGenericDescriptor_ENDPOINT,
USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, CCID_EPT_DATA_IN ),
USBEndpointDescriptor_BULK,
MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_DATA_IN),
USBEndpointDescriptor_MAXBULKSIZE_FS),
0x00 // Does not apply to Bulk endpoints
},
// Notification endpoint descriptor
{
sizeof(USBEndpointDescriptor),
USBGenericDescriptor_ENDPOINT,
USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, CCID_EPT_NOTIFICATION ),
USBEndpointDescriptor_INTERRUPT,
MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_NOTIFICATION),
USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
0x10
},
};
/* SIM card emulator */
typedef struct _SIMTraceDriverConfigurationDescriptorPhone {
/** Standard configuration descriptor. */
USBConfigurationDescriptor configuration;
USBInterfaceDescriptor phone;
USBEndpointDescriptor phone_dataOut;
USBEndpointDescriptor phone_dataIn;
USBEndpointDescriptor phone_interruptIn;
} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorPhone;
const SIMTraceDriverConfigurationDescriptorPhone configurationDescriptorPhone = {
/* Standard configuration descriptor */
{
sizeof(USBConfigurationDescriptor),
USBGenericDescriptor_CONFIGURATION,
sizeof(SIMTraceDriverConfigurationDescriptorPhone),
1, /* There is one interface in this configuration */
CFG_NUM_PHONE, /* configuration number */
PHONE_CONF_STR, /* string descriptor for this configuration */
USBD_BMATTRIBUTES,
USBConfigurationDescriptor_POWER(100)
},
/* Communication class interface standard descriptor */
{
sizeof(USBInterfaceDescriptor),
USBGenericDescriptor_INTERFACE,
0, /* This is interface #0 */
0, /* This is alternate setting #0 for this interface */
3, /* Number of endpoints */
0xff, /* Descriptor Class: Vendor specific */
0, /* No subclass */
0, /* No l */
PHONE_CONF_STR /* Third in string descriptor for this interface */
},
/* Bulk-OUT endpoint standard descriptor */
{
sizeof(USBEndpointDescriptor),
USBGenericDescriptor_ENDPOINT,
USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
PHONE_DATAOUT),
USBEndpointDescriptor_BULK,
MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAOUT),
USBEndpointDescriptor_MAXBULKSIZE_FS),
0 /* Must be 0 for full-speed bulk endpoints */
},
/* Bulk-IN endpoint descriptor */
{
sizeof(USBEndpointDescriptor),
USBGenericDescriptor_ENDPOINT,
USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
PHONE_DATAIN),
USBEndpointDescriptor_BULK,
MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAIN),
USBEndpointDescriptor_MAXBULKSIZE_FS),
0 /* Must be 0 for full-speed bulk endpoints */
},
/* Notification endpoint descriptor */
{
sizeof(USBEndpointDescriptor),
USBGenericDescriptor_ENDPOINT,
USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, PHONE_INT ),
USBEndpointDescriptor_INTERRUPT,
MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_INT),
USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
0x10
}
};
typedef struct _SIMTraceDriverConfigurationDescriptorMITM {
/** Standard configuration descriptor. */
USBConfigurationDescriptor configuration;
USBInterfaceDescriptor simcard;
/// CCID descriptor
CCIDDescriptor ccid;
/// Bulk OUT endpoint descriptor
USBEndpointDescriptor simcard_dataOut;
/// Bulk IN endpoint descriptor
USBEndpointDescriptor simcard_dataIn;
/// Interrupt OUT endpoint descriptor
USBEndpointDescriptor simcard_interruptIn;
USBInterfaceDescriptor phone;
USBEndpointDescriptor phone_dataOut;
USBEndpointDescriptor phone_dataIn;
USBEndpointDescriptor phone_interruptIn;
} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorMITM;
const SIMTraceDriverConfigurationDescriptorMITM configurationDescriptorMITM = {
/* Standard configuration descriptor */
{
sizeof(USBConfigurationDescriptor),
USBGenericDescriptor_CONFIGURATION,
sizeof(SIMTraceDriverConfigurationDescriptorMITM),
2, /* There are two interfaces in this configuration */
CFG_NUM_MITM, /* configuration number */
MITM_CONF_STR, /* string descriptor for this configuration */
USBD_BMATTRIBUTES,
USBConfigurationDescriptor_POWER(100)
},
// CCID interface descriptor
// Table 4.3-1 Interface Descriptor
// Interface descriptor
{
sizeof(USBInterfaceDescriptor),
USBGenericDescriptor_INTERFACE,
0, // Interface 0
0, // No alternate settings
3, // uses bulk-IN, bulk-OUT and interrupt IN
SMART_CARD_DEVICE_CLASS,
0, // Subclass code
0, // bulk transfers optional interrupt-IN
CCID_CONF_STR // associated string descriptor
},
{
sizeof(CCIDDescriptor), // bLength: Size of this descriptor in bytes
CCID_DECRIPTOR_TYPE, // bDescriptorType:Functional descriptor type
CCID1_10, // bcdCCID: CCID version
0, // bMaxSlotIndex: Value 0 indicates that one slot is supported
VOLTS_5_0, // bVoltageSupport
(1 << PROTOCOL_TO), // dwProtocols
3580, // dwDefaultClock
3580, // dwMaxClock
0, // bNumClockSupported
9600, // dwDataRate : 9600 bauds
9600, // dwMaxDataRate : 9600 bauds
0, // bNumDataRatesSupported
0xfe, // dwMaxIFSD
0, // dwSynchProtocols
0, // dwMechanical
//0x00010042, // dwFeatures: Short APDU level exchanges
CCID_FEATURES_AUTO_CLOCK | CCID_FEATURES_AUTO_BAUD |
CCID_FEATURES_AUTO_PCONF | CCID_FEATURES_AUTO_PNEGO | CCID_FEATURES_EXC_TPDU,
0x0000010F, // dwMaxCCIDMessageLength: For extended APDU level the value shall be between 261 + 10
0xFF, // bClassGetResponse: Echoes the class of the APDU
0xFF, // bClassEnvelope: Echoes the class of the APDU
0, // wLcdLayout: no LCD
0, // bPINSupport: No PIN
1 // bMaxCCIDBusySlot
},
// Bulk-OUT endpoint descriptor
{
sizeof(USBEndpointDescriptor),
USBGenericDescriptor_ENDPOINT,
USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_OUT, CCID_EPT_DATA_OUT ),
USBEndpointDescriptor_BULK,
MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_DATA_OUT),
USBEndpointDescriptor_MAXBULKSIZE_FS),
0x00 // Does not apply to Bulk endpoints
},
// Bulk-IN endpoint descriptor
{
sizeof(USBEndpointDescriptor),
USBGenericDescriptor_ENDPOINT,
USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, CCID_EPT_DATA_IN ),
USBEndpointDescriptor_BULK,
MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_DATA_IN),
USBEndpointDescriptor_MAXBULKSIZE_FS),
0x00 // Does not apply to Bulk endpoints
},
// Notification endpoint descriptor
{
sizeof(USBEndpointDescriptor),
USBGenericDescriptor_ENDPOINT,
USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, CCID_EPT_NOTIFICATION ),
USBEndpointDescriptor_INTERRUPT,
MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(CCID_EPT_NOTIFICATION),
USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
0x10
},
/* Communication class interface standard descriptor */
{
sizeof(USBInterfaceDescriptor),
USBGenericDescriptor_INTERFACE,
1, /* This is interface #1 */
0, /* This is alternate setting #0 for this interface */
3, /* Number of endpoints */
0xff,
0,
0,
0, /* FIXME: string descriptor for this interface */
},
/* Bulk-OUT endpoint standard descriptor */
{
sizeof(USBEndpointDescriptor),
USBGenericDescriptor_ENDPOINT,
USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
PHONE_DATAOUT),
USBEndpointDescriptor_BULK,
MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAOUT),
USBEndpointDescriptor_MAXBULKSIZE_FS),
0 /* Must be 0 for full-speed bulk endpoints */
},
/* Bulk-IN endpoint descriptor */
{
sizeof(USBEndpointDescriptor),
USBGenericDescriptor_ENDPOINT,
USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
PHONE_DATAIN),
USBEndpointDescriptor_BULK,
MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAIN),
USBEndpointDescriptor_MAXBULKSIZE_FS),
0 /* Must be 0 for full-speed bulk endpoints */
},
/* Notification endpoint descriptor */
{
sizeof(USBEndpointDescriptor),
USBGenericDescriptor_ENDPOINT,
USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, PHONE_INT ),
USBEndpointDescriptor_INTERRUPT,
MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_INT),
USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
0x10
}
};
/** Standard USB device descriptor for the CDC serial driver */
const USBDeviceDescriptor deviceDescriptor = {
sizeof(USBDeviceDescriptor),
USBGenericDescriptor_DEVICE,
USBDeviceDescriptor_USB2_00,
0xff,
// CDCDeviceDescriptor_CLASS,
0xff,
// CDCDeviceDescriptor_SUBCLASS,
0xff,
// CDCDeviceDescriptor_PROTOCOL,
BOARD_USB_ENDPOINTS_MAXPACKETSIZE(0),
ATMEL_VENDOR_ID,
SIMTRACE_PRODUCT_ID,
1, /* Release number */
0, /* No string descriptor for manufacturer */
PRODUCT_STRING, /* Index of product string descriptor */
0, /* No string descriptor for serial number */
4 /* Device has 4 possible configurations */
};
const USBConfigurationDescriptor *configurationDescriptorsArr[] = {
&configurationDescriptorSniffer.configuration,
&configurationDescriptorCCID.configuration,
&configurationDescriptorPhone.configuration,
&configurationDescriptorMITM.configuration,
};
/* AT91SAM3S does only support full speed, but not high speed USB */
const USBDDriverDescriptors driverDescriptors = {
&deviceDescriptor,
(const USBConfigurationDescriptor **) &(configurationDescriptorsArr), /* first full-speed configuration descriptor */
0, /* No full-speed device qualifier descriptor */
0, /* No full-speed other speed configuration */
0, /* No high-speed device descriptor */
0, /* No high-speed configuration descriptor */
0, /* No high-speed device qualifier descriptor */
0, /* No high-speed other speed configuration descriptor */
stringDescriptors,
STRING_DESC_CNT /* cnt string descriptors in list */
};
/*----------------------------------------------------------------------------
* Callbacks
*----------------------------------------------------------------------------*/
extern uint8_t conf_changed;
extern uint8_t simtrace_config;
void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum)
{
TRACE_INFO_WP("cfgChanged%d ", cfgnum);
conf_changed =1;
simtrace_config = cfgnum;
}
/*----------------------------------------------------------------------------
* Functions
*----------------------------------------------------------------------------*/
/**
* \brief Configure 48MHz Clock for USB
*/
static void _ConfigureUsbClock(void)
{
/* Enable PLLB for USB */
// FIXME: are these the dividers I actually need?
// FIXME: I could just use PLLA, since it has a frequ of 48Mhz anyways?
PMC->CKGR_PLLBR = CKGR_PLLBR_DIVB(5)
| CKGR_PLLBR_MULB(0xc) /* MULT+1=0xd*/
| CKGR_PLLBR_PLLBCOUNT_Msk;
while((PMC->PMC_SR & PMC_SR_LOCKB) == 0);
/* USB Clock uses PLLB */
PMC->PMC_USB = PMC_USB_USBDIV(0) /* /1 (no divider) */
| PMC_USB_USBS; /* PLLB */
}
void SIMtrace_USB_Initialize( void )
{
_ConfigureUsbClock();
// Get std USB driver
USBDDriver *pUsbd = USBD_GetDriver();
TRACE_DEBUG(".");
// Initialize standard USB driver
USBDDriver_Initialize(pUsbd,
&driverDescriptors,
// FIXME: 2 interface settings supported in MITM mode
0); // Multiple interface settings not supported
USBD_Init();
USBD_Connect();
NVIC_EnableIRQ( UDP_IRQn );
}