Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 1 | /* ---------------------------------------------------------------------------- |
| 2 | * ATMEL Microcontroller Software Support |
| 3 | * ---------------------------------------------------------------------------- |
| 4 | * Copyright (c) 2009, Atmel Corporation |
| 5 | * |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
| 11 | * - Redistributions of source code must retain the above copyright notice, |
| 12 | * this list of conditions and the disclaimer below. |
| 13 | * |
| 14 | * Atmel's name may not be used to endorse or promote products derived from |
| 15 | * this software without specific prior written permission. |
| 16 | * |
| 17 | * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR |
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE |
| 20 | * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
| 23 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 24 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 25 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 26 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | * ---------------------------------------------------------------------------- |
| 28 | */ |
| 29 | |
| 30 | /** |
| 31 | * \file |
| 32 | * |
| 33 | * \section Purpose |
| 34 | * |
| 35 | * ISO 7816 driver |
| 36 | * |
| 37 | * \section Usage |
| 38 | * |
| 39 | * Explanation on the usage of the code made available through the header file. |
| 40 | */ |
| 41 | |
| 42 | /*------------------------------------------------------------------------------ |
| 43 | * Headers |
| 44 | *------------------------------------------------------------------------------*/ |
| 45 | |
| 46 | #include "board.h" |
| 47 | |
| 48 | /*------------------------------------------------------------------------------ |
| 49 | * Definitions |
| 50 | *------------------------------------------------------------------------------*/ |
| 51 | /** Case for APDU commands*/ |
| 52 | #define CASE1 1 |
| 53 | #define CASE2 2 |
| 54 | #define CASE3 3 |
| 55 | |
| 56 | /** Flip flop for send and receive char */ |
| 57 | #define USART_SEND 0 |
| 58 | #define USART_RCV 1 |
| 59 | |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 60 | /*----------------------------------------------------------------------------- |
| 61 | * Internal variables |
| 62 | *-----------------------------------------------------------------------------*/ |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 63 | /** Pin reset master card */ |
Christina Quast | 2fcef41 | 2015-03-10 15:51:21 +0100 | [diff] [blame] | 64 | static Pin *st_pinIso7816RstMC; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 65 | |
Christina Quast | ec9c09e | 2015-04-16 10:45:39 +0200 | [diff] [blame] | 66 | struct Usart_info usart_sim = {.base = USART_SIM, .id = ID_USART_SIM, .state = USART_RCV}; |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 67 | |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 68 | /*---------------------------------------------------------------------------- |
| 69 | * Internal functions |
| 70 | *----------------------------------------------------------------------------*/ |
| 71 | |
| 72 | /** |
| 73 | * Get a character from ISO7816 |
| 74 | * \param pCharToReceive Pointer for store the received char |
| 75 | * \return 0: if timeout else status of US_CSR |
| 76 | */ |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 77 | uint32_t ISO7816_GetChar( uint8_t *pCharToReceive, Usart_info *usart) |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 78 | { |
| 79 | uint32_t status; |
| 80 | uint32_t timeout=0; |
| 81 | |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 82 | Usart *us_base = usart->base; |
| 83 | uint32_t us_id = usart->id; |
| 84 | |
Christina Quast | ec9c09e | 2015-04-16 10:45:39 +0200 | [diff] [blame] | 85 | if( usart->state == USART_SEND ) { |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 86 | while((us_base->US_CSR & US_CSR_TXEMPTY) == 0) {} |
| 87 | us_base->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK; |
Christina Quast | ec9c09e | 2015-04-16 10:45:39 +0200 | [diff] [blame] | 88 | usart->state = USART_RCV; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* Wait USART ready for reception */ |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 92 | while( ((us_base->US_CSR & US_CSR_RXRDY) == 0) ) { |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 93 | if(timeout++ > 12000 * (BOARD_MCK/1000000)) { |
| 94 | TRACE_WARNING("TimeOut\n\r"); |
| 95 | return( 0 ); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /* At least one complete character has been received and US_RHR has not yet been read. */ |
| 100 | |
| 101 | /* Get a char */ |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 102 | *pCharToReceive = ((us_base->US_RHR) & 0xFF); |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 103 | |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 104 | status = (us_base->US_CSR&(US_CSR_OVRE|US_CSR_FRAME| |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 105 | US_CSR_PARE|US_CSR_TIMEOUT|US_CSR_NACK| |
| 106 | (1<<10))); |
| 107 | |
| 108 | if (status != 0 ) { |
| 109 | TRACE_DEBUG("R:0x%" PRIX32 "\n\r", status); |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 110 | TRACE_DEBUG("R:0x%" PRIX32 "\n\r", us_base->US_CSR); |
| 111 | TRACE_DEBUG("Nb:0x%" PRIX32 "\n\r", us_base->US_NER ); |
| 112 | us_base->US_CR = US_CR_RSTSTA; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* Return status */ |
| 116 | return( status ); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /** |
| 121 | * Send a char to ISO7816 |
| 122 | * \param CharToSend char to be send |
| 123 | * \return status of US_CSR |
| 124 | */ |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 125 | uint32_t ISO7816_SendChar( uint8_t CharToSend, Usart_info *usart ) |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 126 | { |
| 127 | uint32_t status; |
| 128 | |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 129 | Usart *us_base = usart->base; |
| 130 | uint32_t us_id = usart->id; |
| 131 | |
Christina Quast | ec9c09e | 2015-04-16 10:45:39 +0200 | [diff] [blame] | 132 | if( usart->state == USART_RCV ) { |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 133 | us_base->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK; |
Christina Quast | ec9c09e | 2015-04-16 10:45:39 +0200 | [diff] [blame] | 134 | usart->state = USART_SEND; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* Wait USART ready for transmit */ |
Christina Quast | 68cc859 | 2015-04-16 11:08:32 +0200 | [diff] [blame] | 138 | int i = 0; |
| 139 | while((us_base->US_CSR & (US_CSR_TXRDY)) == 0) { |
| 140 | i++; |
| 141 | if (!(i%1000000)) { |
Harald Welte | 40901a0 | 2016-03-03 10:42:45 +0100 | [diff] [blame] | 142 | printf("s: %x ", us_base->US_CSR); |
| 143 | printf("s: %x\r\n", us_base->US_RHR & 0xFF); |
Christina Quast | 68cc859 | 2015-04-16 11:08:32 +0200 | [diff] [blame] | 144 | us_base->US_CR = US_CR_RSTTX; |
| 145 | us_base->US_CR = US_CR_RSTRX; |
| 146 | } |
| 147 | } |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 148 | /* There is no character in the US_THR */ |
| 149 | |
| 150 | /* Transmit a char */ |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 151 | us_base->US_THR = CharToSend; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 152 | |
Harald Welte | 40901a0 | 2016-03-03 10:42:45 +0100 | [diff] [blame] | 153 | TRACE_ERROR("Sx%02X\r\n", CharToSend); |
| 154 | |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 155 | status = (us_base->US_CSR&(US_CSR_OVRE|US_CSR_FRAME| |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 156 | US_CSR_PARE|US_CSR_TIMEOUT|US_CSR_NACK| |
| 157 | (1<<10))); |
| 158 | |
| 159 | if (status != 0 ) { |
Christina Quast | 68cc859 | 2015-04-16 11:08:32 +0200 | [diff] [blame] | 160 | TRACE_INFO("******* status: 0x%" PRIX32 " (Overrun: %" PRIX32 |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 161 | ", NACK: %" PRIX32 ", Timeout: %" PRIX32 ", underrun: %" PRIX32 ")\n\r", |
| 162 | status, ((status & US_CSR_OVRE)>> 5), ((status & US_CSR_NACK) >> 13), |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 163 | ((status & US_CSR_TIMEOUT) >> 8), ((status & (1 << 10)) >> 10)); |
Christina Quast | 68cc859 | 2015-04-16 11:08:32 +0200 | [diff] [blame] | 164 | TRACE_INFO("E (USART CSR reg):0x%" PRIX32 "\n\r", us_base->US_CSR); |
| 165 | TRACE_INFO("Nb (Number of errors):0x%" PRIX32 "\n\r", us_base->US_NER ); |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 166 | us_base->US_CR = US_CR_RSTSTA; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /* Return status */ |
| 170 | return( status ); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | /** |
| 175 | * Iso 7816 ICC power on |
| 176 | */ |
| 177 | static void ISO7816_IccPowerOn( void ) |
| 178 | { |
| 179 | /* Set RESET Master Card */ |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 180 | if (st_pinIso7816RstMC) { |
| 181 | PIO_Set(st_pinIso7816RstMC); |
| 182 | } |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /*---------------------------------------------------------------------------- |
| 186 | * Exported functions |
| 187 | *----------------------------------------------------------------------------*/ |
| 188 | |
| 189 | /** |
| 190 | * Iso 7816 ICC power off |
| 191 | */ |
| 192 | void ISO7816_IccPowerOff( void ) |
| 193 | { |
| 194 | /* Clear RESET Master Card */ |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 195 | if (st_pinIso7816RstMC) { |
| 196 | PIO_Clear(st_pinIso7816RstMC); |
| 197 | } |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /** |
Christina Quast | 73c2b64 | 2015-04-07 13:49:26 +0200 | [diff] [blame] | 201 | * Transfert Block TPDU T=0 |
| 202 | * \param pAPDU APDU buffer |
| 203 | * \param pMessage Message buffer |
| 204 | * \param wLength Block length |
| 205 | * \param indexMsg Message index |
| 206 | * \return 0 on success, content of US_CSR otherwise |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 207 | */ |
Christina Quast | 73c2b64 | 2015-04-07 13:49:26 +0200 | [diff] [blame] | 208 | uint32_t ISO7816_XfrBlockTPDU_T0(const uint8_t *pAPDU, |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 209 | uint8_t *pMessage, |
Christina Quast | 73c2b64 | 2015-04-07 13:49:26 +0200 | [diff] [blame] | 210 | uint16_t wLength, |
| 211 | uint16_t *retlen ) |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 212 | { |
| 213 | uint16_t NeNc; |
| 214 | uint16_t indexApdu = 4; |
Christina Quast | 73c2b64 | 2015-04-07 13:49:26 +0200 | [diff] [blame] | 215 | uint16_t indexMsg = 0; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 216 | uint8_t SW1 = 0; |
| 217 | uint8_t procByte; |
| 218 | uint8_t cmdCase; |
Christina Quast | 73c2b64 | 2015-04-07 13:49:26 +0200 | [diff] [blame] | 219 | uint32_t status = 0; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 220 | |
Christina Quast | c63df59 | 2015-03-10 23:32:45 +0100 | [diff] [blame] | 221 | TRACE_INFO("pAPDU[0]=0x%X\n\r",pAPDU[0]); |
| 222 | TRACE_INFO("pAPDU[1]=0x%X\n\r",pAPDU[1]); |
| 223 | TRACE_INFO("pAPDU[2]=0x%X\n\r",pAPDU[2]); |
| 224 | TRACE_INFO("pAPDU[3]=0x%X\n\r",pAPDU[3]); |
| 225 | TRACE_INFO("pAPDU[4]=0x%X\n\r",pAPDU[4]); |
| 226 | TRACE_INFO("pAPDU[5]=0x%X\n\r",pAPDU[5]); |
| 227 | TRACE_INFO("wlength=%d\n\r",wLength); |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 228 | |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 229 | ISO7816_SendChar( pAPDU[0], &usart_sim ); /* CLA */ |
| 230 | ISO7816_SendChar( pAPDU[1], &usart_sim ); /* INS */ |
| 231 | ISO7816_SendChar( pAPDU[2], &usart_sim ); /* P1 */ |
| 232 | ISO7816_SendChar( pAPDU[3], &usart_sim ); /* P2 */ |
| 233 | ISO7816_SendChar( pAPDU[4], &usart_sim ); /* P3 */ |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 234 | |
| 235 | /* Handle the four structures of command APDU */ |
Christina Quast | c63df59 | 2015-03-10 23:32:45 +0100 | [diff] [blame] | 236 | indexApdu = 5; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 237 | |
| 238 | if( wLength == 4 ) { |
| 239 | cmdCase = CASE1; |
| 240 | NeNc = 0; |
| 241 | } |
| 242 | else if( wLength == 5) { |
| 243 | cmdCase = CASE2; |
| 244 | NeNc = pAPDU[4]; /* C5 */ |
| 245 | if (NeNc == 0) { |
| 246 | NeNc = 256; |
| 247 | } |
| 248 | } |
| 249 | else if( wLength == 6) { |
| 250 | NeNc = pAPDU[4]; /* C5 */ |
| 251 | cmdCase = CASE3; |
| 252 | } |
| 253 | else if( wLength == 7) { |
| 254 | NeNc = pAPDU[4]; /* C5 */ |
| 255 | if( NeNc == 0 ) { |
| 256 | cmdCase = CASE2; |
| 257 | NeNc = (pAPDU[5]<<8)+pAPDU[6]; |
| 258 | } |
| 259 | else { |
| 260 | cmdCase = CASE3; |
| 261 | } |
| 262 | } |
| 263 | else { |
| 264 | NeNc = pAPDU[4]; /* C5 */ |
| 265 | if( NeNc == 0 ) { |
| 266 | cmdCase = CASE3; |
| 267 | NeNc = (pAPDU[5]<<8)+pAPDU[6]; |
| 268 | } |
| 269 | else { |
| 270 | cmdCase = CASE3; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | TRACE_DEBUG("CASE=0x%X NeNc=0x%X\n\r", cmdCase, NeNc); |
| 275 | |
| 276 | /* Handle Procedure Bytes */ |
| 277 | do { |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 278 | status = ISO7816_GetChar(&procByte, &usart_sim); |
Christina Quast | 73c2b64 | 2015-04-07 13:49:26 +0200 | [diff] [blame] | 279 | if (status != 0) { |
| 280 | return status; |
| 281 | } |
Christina Quast | 2fcef41 | 2015-03-10 15:51:21 +0100 | [diff] [blame] | 282 | TRACE_INFO("procByte: 0x%X\n\r", procByte); |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 283 | /* Handle NULL */ |
| 284 | if ( procByte == ISO_NULL_VAL ) { |
Christina Quast | 2fcef41 | 2015-03-10 15:51:21 +0100 | [diff] [blame] | 285 | TRACE_INFO("INS\n\r"); |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 286 | continue; |
| 287 | } |
| 288 | /* Handle SW1 */ |
| 289 | else if ( ((procByte & 0xF0) ==0x60) || ((procByte & 0xF0) ==0x90) ) { |
Christina Quast | 2fcef41 | 2015-03-10 15:51:21 +0100 | [diff] [blame] | 290 | TRACE_INFO("SW1\n\r"); |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 291 | SW1 = 1; |
| 292 | } |
| 293 | /* Handle INS */ |
| 294 | else if ( pAPDU[1] == procByte) { |
Christina Quast | 2fcef41 | 2015-03-10 15:51:21 +0100 | [diff] [blame] | 295 | TRACE_INFO("HdlINS\n\r"); |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 296 | if (cmdCase == CASE2) { |
| 297 | /* receive data from card */ |
| 298 | do { |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 299 | status = ISO7816_GetChar(&pMessage[indexMsg++], &usart_sim); |
Christina Quast | 73c2b64 | 2015-04-07 13:49:26 +0200 | [diff] [blame] | 300 | } while(( 0 != --NeNc) && (status == 0) ); |
| 301 | if (status != 0) { |
| 302 | return status; |
| 303 | } |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 304 | } |
| 305 | else { |
| 306 | /* Send data */ |
| 307 | do { |
Christina Quast | c63df59 | 2015-03-10 23:32:45 +0100 | [diff] [blame] | 308 | TRACE_INFO("Send %X", pAPDU[indexApdu]); |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 309 | ISO7816_SendChar(pAPDU[indexApdu++], &usart_sim); |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 310 | } while( 0 != --NeNc ); |
| 311 | } |
| 312 | } |
| 313 | /* Handle INS ^ 0xff */ |
Christina Quast | 767fdeb | 2015-04-07 13:50:20 +0200 | [diff] [blame] | 314 | else |
| 315 | #pragma GCC diagnostic push |
| 316 | #pragma GCC diagnostic ignored "-Wsign-compare" |
| 317 | if ( pAPDU[1] == (procByte ^ 0xff)) { |
| 318 | #pragma GCC diagnostic pop |
Christina Quast | 2fcef41 | 2015-03-10 15:51:21 +0100 | [diff] [blame] | 319 | TRACE_INFO("HdlINS+\n\r"); |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 320 | if (cmdCase == CASE2) { |
| 321 | /* receive data from card */ |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 322 | status = ISO7816_GetChar(&pMessage[indexMsg++], &usart_sim); |
Christina Quast | 73c2b64 | 2015-04-07 13:49:26 +0200 | [diff] [blame] | 323 | if (status != 0) { |
| 324 | return status; |
| 325 | } |
| 326 | TRACE_INFO("Rcv: 0x%X\n\r", pMessage[indexMsg-1]); |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 327 | } |
| 328 | else { |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 329 | status = ISO7816_SendChar(pAPDU[indexApdu++], &usart_sim); |
Christina Quast | 73c2b64 | 2015-04-07 13:49:26 +0200 | [diff] [blame] | 330 | if (status != 0) { |
| 331 | return status; |
| 332 | } |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 333 | } |
| 334 | NeNc--; |
| 335 | } |
| 336 | else { |
| 337 | /* ?? */ |
Christina Quast | 2fcef41 | 2015-03-10 15:51:21 +0100 | [diff] [blame] | 338 | TRACE_INFO("procByte=0x%X\n\r", procByte); |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 339 | break; |
| 340 | } |
| 341 | } while (NeNc != 0); |
| 342 | |
| 343 | /* Status Bytes */ |
| 344 | if (SW1 == 0) { |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 345 | status = ISO7816_GetChar(&pMessage[indexMsg++], &usart_sim); /* SW1 */ |
Christina Quast | 73c2b64 | 2015-04-07 13:49:26 +0200 | [diff] [blame] | 346 | if (status != 0) { |
| 347 | return status; |
| 348 | } |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 349 | } |
| 350 | else { |
Christina Quast | 73c2b64 | 2015-04-07 13:49:26 +0200 | [diff] [blame] | 351 | pMessage[indexMsg++] = procByte; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 352 | } |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 353 | status = ISO7816_GetChar(&pMessage[indexMsg++], &usart_sim); /* SW2 */ |
Christina Quast | 73c2b64 | 2015-04-07 13:49:26 +0200 | [diff] [blame] | 354 | if (status != 0) { |
| 355 | return status; |
| 356 | } |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 357 | |
Christina Quast | 73c2b64 | 2015-04-07 13:49:26 +0200 | [diff] [blame] | 358 | TRACE_WARNING("SW1=0x%X, SW2=0x%X\n\r", pMessage[indexMsg-2], pMessage[indexMsg-1]); |
Christina Quast | 2fcef41 | 2015-03-10 15:51:21 +0100 | [diff] [blame] | 359 | |
Christina Quast | 73c2b64 | 2015-04-07 13:49:26 +0200 | [diff] [blame] | 360 | *retlen = indexMsg; |
| 361 | return status; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 362 | |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Escape ISO7816 |
| 367 | */ |
| 368 | void ISO7816_Escape( void ) |
| 369 | { |
| 370 | TRACE_DEBUG("For user, if needed\n\r"); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Restart clock ISO7816 |
| 375 | */ |
| 376 | void ISO7816_RestartClock( void ) |
| 377 | { |
| 378 | TRACE_DEBUG("ISO7816_RestartClock\n\r"); |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 379 | USART_SIM->US_BRGR = 13; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Stop clock ISO7816 |
| 384 | */ |
| 385 | void ISO7816_StopClock( void ) |
| 386 | { |
| 387 | TRACE_DEBUG("ISO7816_StopClock\n\r"); |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 388 | USART_SIM->US_BRGR = 0; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /** |
| 392 | * T0 APDU |
| 393 | */ |
| 394 | void ISO7816_toAPDU( void ) |
| 395 | { |
| 396 | TRACE_DEBUG("ISO7816_toAPDU\n\r"); |
| 397 | TRACE_DEBUG("Not supported at this time\n\r"); |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Answer To Reset (ATR) |
| 402 | * \param pAtr ATR buffer |
| 403 | * \param pLength Pointer for store the ATR length |
| 404 | * \return 0: if timeout else status of US_CSR |
| 405 | */ |
| 406 | uint32_t ISO7816_Datablock_ATR( uint8_t* pAtr, uint8_t* pLength ) |
| 407 | { |
| 408 | uint32_t i; |
| 409 | uint32_t j; |
| 410 | uint32_t y; |
| 411 | uint32_t status = 0; |
| 412 | |
| 413 | *pLength = 0; |
| 414 | |
| 415 | /* Read ATR TS */ |
| 416 | // FIXME: There should always be a check for the GetChar return value..0 means timeout |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 417 | status = ISO7816_GetChar(&pAtr[0], &usart_sim); |
Christina Quast | 73c2b64 | 2015-04-07 13:49:26 +0200 | [diff] [blame] | 418 | if (status != 0) { |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 419 | return status; |
Christina Quast | 73c2b64 | 2015-04-07 13:49:26 +0200 | [diff] [blame] | 420 | } |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 421 | |
| 422 | /* Read ATR T0 */ |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 423 | status = ISO7816_GetChar(&pAtr[1], &usart_sim); |
Christina Quast | 73c2b64 | 2015-04-07 13:49:26 +0200 | [diff] [blame] | 424 | if (status != 0) { |
| 425 | return status; |
| 426 | } |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 427 | y = pAtr[1] & 0xF0; |
| 428 | i = 2; |
| 429 | |
| 430 | /* Read ATR Ti */ |
Christina Quast | 73c2b64 | 2015-04-07 13:49:26 +0200 | [diff] [blame] | 431 | while (y && (status == 0)) { |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 432 | |
| 433 | if (y & 0x10) { /* TA[i] */ |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 434 | status = ISO7816_GetChar(&pAtr[i++], &usart_sim); |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 435 | } |
| 436 | if (y & 0x20) { /* TB[i] */ |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 437 | status = ISO7816_GetChar(&pAtr[i++], &usart_sim); |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 438 | } |
| 439 | if (y & 0x40) { /* TC[i] */ |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 440 | status = ISO7816_GetChar(&pAtr[i++], &usart_sim); |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 441 | } |
| 442 | if (y & 0x80) { /* TD[i] */ |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 443 | status = ISO7816_GetChar(&pAtr[i], &usart_sim); |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 444 | y = pAtr[i++] & 0xF0; |
| 445 | } |
| 446 | else { |
| 447 | y = 0; |
| 448 | } |
| 449 | } |
Christina Quast | 73c2b64 | 2015-04-07 13:49:26 +0200 | [diff] [blame] | 450 | if (status != 0) { |
| 451 | return status; |
| 452 | } |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 453 | |
| 454 | /* Historical Bytes */ |
| 455 | y = pAtr[1] & 0x0F; |
Christina Quast | 73c2b64 | 2015-04-07 13:49:26 +0200 | [diff] [blame] | 456 | for( j=0; (j < y) && (status == 0); j++ ) { |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 457 | status = ISO7816_GetChar(&pAtr[i++], &usart_sim); |
Christina Quast | 73c2b64 | 2015-04-07 13:49:26 +0200 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | if (status != 0) { |
| 461 | return status; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | *pLength = i; |
Christina Quast | 73c2b64 | 2015-04-07 13:49:26 +0200 | [diff] [blame] | 465 | return status; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Set data rate and clock frequency |
| 470 | * \param dwClockFrequency ICC clock frequency in KHz. |
| 471 | * \param dwDataRate ICC data rate in bpd |
| 472 | */ |
| 473 | void ISO7816_SetDataRateandClockFrequency( uint32_t dwClockFrequency, uint32_t dwDataRate ) |
| 474 | { |
| 475 | uint8_t ClockFrequency; |
| 476 | |
| 477 | /* Define the baud rate divisor register */ |
| 478 | /* CD = MCK / SCK */ |
| 479 | /* SCK = FIDI x BAUD = 372 x 9600 */ |
| 480 | /* BOARD_MCK */ |
| 481 | /* CD = MCK/(FIDI x BAUD) = 48000000 / (372x9600) = 13 */ |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 482 | USART_SIM->US_BRGR = BOARD_MCK / (dwClockFrequency*1000); |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 483 | |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 484 | ClockFrequency = BOARD_MCK / USART_SIM->US_BRGR; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 485 | |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 486 | USART_SIM->US_FIDI = (ClockFrequency)/dwDataRate; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 487 | |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Pin status for ISO7816 RESET |
| 492 | * \return 1 if the Pin RstMC is high; otherwise 0. |
| 493 | */ |
| 494 | uint8_t ISO7816_StatusReset( void ) |
| 495 | { |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 496 | if (st_pinIso7816RstMC) { |
| 497 | return PIO_Get(st_pinIso7816RstMC); |
| 498 | } |
| 499 | return 0; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /** |
| 503 | * cold reset |
| 504 | */ |
| 505 | void ISO7816_cold_reset( void ) |
| 506 | { |
| 507 | volatile uint32_t i; |
| 508 | |
Christina Quast | c870b52 | 2015-04-09 13:31:58 +0200 | [diff] [blame] | 509 | /* tb: wait ??? cycles*/ |
| 510 | for( i=0; i<(400*(BOARD_MCK/1000000)); i++ ) { |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 511 | } |
| 512 | |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 513 | USART_SIM->US_RHR; |
| 514 | USART_SIM->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 515 | |
| 516 | ISO7816_IccPowerOn(); |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * Warm reset |
| 521 | */ |
| 522 | void ISO7816_warm_reset( void ) |
| 523 | { |
| 524 | volatile uint32_t i; |
| 525 | |
| 526 | // Clears Reset |
| 527 | ISO7816_IccPowerOff(); |
| 528 | |
Christina Quast | c870b52 | 2015-04-09 13:31:58 +0200 | [diff] [blame] | 529 | /* tb: wait ??? cycles */ |
| 530 | for( i=0; i<(400*(BOARD_MCK/1000000)); i++ ) { |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 531 | } |
| 532 | |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 533 | USART_SIM->US_RHR; |
| 534 | USART_SIM->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 535 | |
| 536 | // Sets Reset |
| 537 | ISO7816_IccPowerOn(); |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * Decode ATR trace |
| 542 | * \param pAtr pointer on ATR buffer |
| 543 | */ |
| 544 | void ISO7816_Decode_ATR( uint8_t* pAtr ) |
| 545 | { |
| 546 | uint32_t i; |
| 547 | uint32_t j; |
| 548 | uint32_t y; |
| 549 | uint8_t offset; |
| 550 | |
| 551 | printf("\n\r"); |
| 552 | printf("ATR: Answer To Reset:\n\r"); |
| 553 | printf("TS = 0x%X Initial character ",pAtr[0]); |
| 554 | if( pAtr[0] == 0x3B ) { |
| 555 | |
| 556 | printf("Direct Convention\n\r"); |
| 557 | } |
| 558 | else { |
| 559 | if( pAtr[0] == 0x3F ) { |
| 560 | |
| 561 | printf("Inverse Convention\n\r"); |
| 562 | } |
| 563 | else { |
| 564 | printf("BAD Convention\n\r"); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | printf("T0 = 0x%X Format caracter\n\r",pAtr[1]); |
| 569 | printf(" Number of historical bytes: K = %d\n\r", pAtr[1]&0x0F); |
| 570 | printf(" Presence further interface byte:\n\r"); |
| 571 | if( pAtr[1]&0x80 ) { |
| 572 | printf("TA "); |
| 573 | } |
| 574 | if( pAtr[1]&0x40 ) { |
| 575 | printf("TB "); |
| 576 | } |
| 577 | if( pAtr[1]&0x20 ) { |
| 578 | printf("TC "); |
| 579 | } |
| 580 | if( pAtr[1]&0x10 ) { |
| 581 | printf("TD "); |
| 582 | } |
| 583 | if( pAtr[1] != 0 ) { |
| 584 | printf(" present\n\r"); |
| 585 | } |
| 586 | |
| 587 | i = 2; |
| 588 | y = pAtr[1] & 0xF0; |
| 589 | |
| 590 | /* Read ATR Ti */ |
| 591 | offset = 1; |
| 592 | while (y) { |
| 593 | |
| 594 | if (y & 0x10) { /* TA[i] */ |
| 595 | printf("TA[%d] = 0x%X ", offset, pAtr[i]); |
| 596 | if( offset == 1 ) { |
| 597 | printf("FI = %d ", (pAtr[i]>>8)); |
| 598 | printf("DI = %d", (pAtr[i]&0x0F)); |
| 599 | } |
| 600 | printf("\n\r"); |
| 601 | i++; |
| 602 | } |
| 603 | if (y & 0x20) { /* TB[i] */ |
| 604 | printf("TB[%d] = 0x%X\n\r", offset, pAtr[i]); |
| 605 | i++; |
| 606 | } |
| 607 | if (y & 0x40) { /* TC[i] */ |
| 608 | printf("TC[%d] = 0x%X ", offset, pAtr[i]); |
| 609 | if( offset == 1 ) { |
| 610 | printf("Extra Guard Time: N = %d", pAtr[i]); |
| 611 | } |
| 612 | printf("\n\r"); |
| 613 | i++; |
| 614 | } |
| 615 | if (y & 0x80) { /* TD[i] */ |
| 616 | printf("TD[%d] = 0x%X\n\r", offset, pAtr[i]); |
| 617 | y = pAtr[i++] & 0xF0; |
| 618 | } |
| 619 | else { |
| 620 | y = 0; |
| 621 | } |
| 622 | offset++; |
| 623 | } |
| 624 | |
| 625 | /* Historical Bytes */ |
| 626 | printf("Historical bytes:\n\r"); |
| 627 | y = pAtr[1] & 0x0F; |
| 628 | for( j=0; j < y; j++ ) { |
| 629 | printf(" 0x%X", pAtr[i]); |
| 630 | i++; |
| 631 | } |
| 632 | printf("\n\r\n\r"); |
| 633 | |
| 634 | } |
| 635 | |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 636 | void ISO7816_Set_Reset_Pin(const Pin *pPinIso7816RstMC) { |
| 637 | /* Pin ISO7816 initialize */ |
| 638 | st_pinIso7816RstMC = (Pin *)pPinIso7816RstMC; |
| 639 | } |
| 640 | |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 641 | /** Initializes a ISO driver |
| 642 | * \param pPinIso7816RstMC Pin ISO 7816 Rst MC |
| 643 | */ |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 644 | void ISO7816_Init( Usart_info *usart, bool master_clock ) |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 645 | { |
Christina Quast | 3eab56e | 2015-04-10 15:38:49 +0200 | [diff] [blame] | 646 | uint32_t clk; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 647 | TRACE_DEBUG("ISO_Init\n\r"); |
| 648 | |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 649 | Usart *us_base = usart->base; |
| 650 | uint32_t us_id = usart->id; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 651 | |
Christina Quast | 3eab56e | 2015-04-10 15:38:49 +0200 | [diff] [blame] | 652 | if (master_clock == true) { |
| 653 | clk = US_MR_USCLKS_MCK; |
| 654 | } else { |
| 655 | clk = US_MR_USCLKS_SCK; |
| 656 | } |
| 657 | |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 658 | USART_Configure( us_base, |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 659 | US_MR_USART_MODE_IS07816_T_0 |
Christina Quast | 3eab56e | 2015-04-10 15:38:49 +0200 | [diff] [blame] | 660 | | clk |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 661 | | US_MR_NBSTOP_1_BIT |
| 662 | | US_MR_PAR_EVEN |
| 663 | | US_MR_CHRL_8_BIT |
| 664 | | US_MR_CLKO |
Christina Quast | 68cc859 | 2015-04-16 11:08:32 +0200 | [diff] [blame] | 665 | | US_MR_INACK /* Inhibit errors */ |
| 666 | | (3<<24), /* MAX_ITERATION */ |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 667 | 1, |
| 668 | 0); |
| 669 | |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 670 | /* Disable interrupts */ |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 671 | us_base->US_IDR = (uint32_t) -1; |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 672 | |
Christina Quast | 68cc859 | 2015-04-16 11:08:32 +0200 | [diff] [blame] | 673 | /* Configure USART */ |
| 674 | PMC_EnablePeripheral(us_id); |
| 675 | |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 676 | us_base->US_FIDI = 372; /* by default */ |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 677 | /* Define the baud rate divisor register */ |
| 678 | /* CD = MCK / SCK */ |
| 679 | /* SCK = FIDI x BAUD = 372 x 9600 */ |
| 680 | /* BOARD_MCK */ |
| 681 | /* CD = MCK/(FIDI x BAUD) = 48000000 / (372x9600) = 13 */ |
Christina Quast | 4b81493 | 2015-04-17 18:47:34 +0200 | [diff] [blame] | 682 | if (master_clock == true) { |
| 683 | us_base->US_BRGR = BOARD_MCK / (372*9600); |
| 684 | } else { |
| 685 | us_base->US_BRGR = US_BRGR_CD(1); |
| 686 | } |
Christina Quast | 32a90ac | 2015-03-10 16:18:16 +0100 | [diff] [blame] | 687 | } |
| 688 | |