Changeset 1389 for trunk/csctapi/icc_async.c
- Timestamp:
- 01/31/10 19:48:55 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/csctapi/icc_async.c
r1381 r1389 35 35 #include "protocol_t1.h" 36 36 37 /* 38 * Not exported constants definition 39 */ 40 #define ICC_ASYNC_MAX_TRANSMIT 255 41 #define ICC_ASYNC_BAUDRATE 9600 37 // Default T0/T14 settings 38 #define DEFAULT_WI 10 39 // Default T1 settings 40 #define DEFAULT_IFSC 32 41 #define MAX_IFSC 251 /* Cannot send > 255 buffer */ 42 #define DEFAULT_CWI 13 43 #define DEFAULT_BWI 4 44 #define EDC_LRC 0 45 46 #define PPS_MAX_LENGTH 6 47 #define PPS_HAS_PPS1(block) ((block[1] & 0x10) == 0x10) 48 #define PPS_HAS_PPS2(block) ((block[1] & 0x20) == 0x20) 49 #define PPS_HAS_PPS3(block) ((block[1] & 0x40) == 0x40) 50 42 51 43 52 /* … … 46 55 47 56 static void ICC_Async_InvertBuffer (unsigned size, BYTE * buffer); 48 int Protocol_Command (APDU_Cmd * cmd, APDU_Rsp ** rsp); 49 extern unsigned int ETU_to_ms(unsigned long WWT); 57 static int Parse_ATR (ATR * atr); 58 static int PPS_Exchange (BYTE * params, unsigned *length); 59 static unsigned PPS_GetLength (BYTE * block); 60 static int InitCard (ATR * atr, BYTE FI, double d, double n); 61 static unsigned int ETU_to_ms(unsigned long WWT); 62 static BYTE PPS_GetPCK (BYTE * block, unsigned length); 63 static int Protocol_Command (APDU_Cmd * cmd, APDU_Rsp ** rsp); 64 static int SetRightParity (void); 50 65 51 66 int fdmc=(-1); … … 57 72 int ICC_Async_Device_Init () 58 73 { 59 74 #if defined(LIBUSB) 75 if (reader[ridx].typ == R_SMART) { 76 if(SR_Init(&reader[ridx])) { 77 return ERROR; 78 } 79 } 80 else 81 #endif 82 { 60 83 wr = 0; 61 84 #ifdef DEBUG_IO … … 68 91 reader[ridx].handle = open (reader[ridx].device, O_RDWR|O_NONBLOCK|O_NOCTTY); 69 92 #elif COOL 70 return Cool_Init();93 return !Cool_Init(); //if cool_init succeeds, it returns 0, so device_init returns 1 ? /FIXME I think the ! should disappear 71 94 #else 72 95 reader[ridx].handle = open (reader[ridx].device, O_RDWR); … … 78 101 79 102 if (reader[ridx].handle < 0) 80 return ICC_ASYNC_IFD_ERROR;103 return ERROR; 81 104 82 105 #if defined(TUXBOX) && defined(PPC) … … 85 108 { 86 109 close(reader[ridx].handle); 87 return ICC_ASYNC_IFD_ERROR;110 return ERROR; 88 111 } 89 112 #endif 90 113 91 114 if (reader[ridx].typ != R_INTERNAL) { //FIXME move to ifd_phoenix.c 92 if( !IO_Serial_InitPnP ())93 return ICC_ASYNC_IFD_ERROR;115 if(IO_Serial_InitPnP ()) 116 return ERROR; 94 117 IO_Serial_Flush(); 95 118 } 96 97 return ICC_ASYNC_OK; 98 } 99 100 int ICC_Async_GetStatus (BYTE * result) 101 { 102 BYTE status[2]; 103 // unsigned int modembits=0; 119 if (Phoenix_Init()) { 120 IO_Serial_Close (); //FIXME no Phoenix_Close ? 121 return ERROR; 122 } 123 } 124 return OK; 125 } 126 127 int ICC_Async_GetStatus (int * card) 128 { 104 129 int in; 105 130 … … 109 134 110 135 #ifdef SCI_DEV 111 if(reader[ridx].typ == R_INTERNAL) 112 { 113 if(!Sci_GetStatus(reader[ridx].handle, &in)) 114 return ICC_ASYNC_IFD_ERROR; 136 if(reader[ridx].typ == R_INTERNAL) { 137 if(Sci_GetStatus(reader[ridx].handle, &in)) 138 return ERROR; 115 139 } 116 140 else 117 141 #elif COOL 118 if(reader[ridx].typ == R_INTERNAL) 119 { 120 if (!Cool_GetStatus(&in)) 121 return ICC_ASYNC_IFD_ERROR; 142 if(reader[ridx].typ == R_INTERNAL) { 143 if (Cool_GetStatus(&in)) 144 return ERROR; 145 } 146 else 147 #endif 148 #if defined(LIBUSB) 149 if(reader[ridx].typ == R_SMART) { 150 if (SR_GetStatus(&reader[ridx],&in)) 151 return ERROR; 122 152 } 123 153 else … … 125 155 126 156 #if defined(TUXBOX) && defined(PPC) 127 if ((reader[ridx].typ == R_DB2COM1) || (reader[ridx].typ == R_DB2COM2)) 128 { 157 if ((reader[ridx].typ == R_DB2COM1) || (reader[ridx].typ == R_DB2COM2)) { 129 158 ushort msr=1; 130 159 extern int fdmc; … … 139 168 else 140 169 #endif 141 if (!Phoenix_GetStatus(&in)) 142 return ICC_ASYNC_IFD_ERROR; 143 144 if (in) 145 { 146 if(reader[ridx].status == 0) 147 { 148 status[0] = IFD_TOWITOKO_CARD_CHANGE; 149 reader[ridx].status = 1; 150 } 151 else if(reader[ridx].status == 1) 152 { 153 status[0] = IFD_TOWITOKO_CARD_NOCHANGE; 154 } 155 else 156 { 157 status[0] = IFD_TOWITOKO_CARD_CHANGE; 158 reader[ridx].status = 1; 159 } 160 } 161 else 162 { 163 if(reader[ridx].status == 0) 164 { 165 status[0] = IFD_TOWITOKO_NOCARD_CHANGE; 166 reader[ridx].status = 2; 167 } 168 else if(reader[ridx].status == 1) 169 { 170 status[0] = IFD_TOWITOKO_NOCARD_CHANGE; 171 reader[ridx].status = 2; 172 } 173 else 174 { 175 status[0] = IFD_TOWITOKO_NOCARD_NOCHANGE; 176 } 177 } 178 179 180 (*result) = status[0]; 170 if (Phoenix_GetStatus(&in)) 171 return ERROR; 172 173 if (in) { 174 *card = TRUE; 175 reader[ridx].status = 1; 176 } 177 else { 178 *card = FALSE; 179 reader[ridx].status = 2; 180 } 181 181 182 182 #ifdef DEBUG_IFD 183 printf ("IFD: com%d Status = %s / %s\n", reader[ridx].typ, IFD_TOWITOKO_CARD(status[0])? "card": "no card", IFD_TOWITOKO_CHANGE(status[0])? "change": "no change"); 184 #endif 185 186 return ICC_ASYNC_OK; 187 } 188 189 int ICC_Async_Init () 190 { 191 #ifndef ICC_TYPE_SYNC 192 unsigned np=0; 193 194 // Initialize Baudrate 195 if (!Phoenix_SetBaudrate (ICC_ASYNC_BAUDRATE)) 196 return ICC_ASYNC_IFD_ERROR; 197 198 atr = &icc_atr; 183 printf ("IFD: com%d Status = %s / %s\n", reader[ridx].typ, in ? "card": "no card", in ? "change": "no change"); 184 #endif 185 186 return OK; 187 } 188 189 int ICC_Async_Activate (ATR * atr) 190 { 191 int card; 192 193 if (ICC_Async_GetStatus (&card)) 194 return ERROR; 195 if (card == 0) 196 return ERROR; 197 198 #if defined(LIBUSB) 199 if (reader[ridx].typ == R_SMART) { 200 if (SR_Reset(&reader[ridx],atr)) { 201 return ERROR; 202 } 203 } 204 else 205 { 206 #endif 207 208 /* Initialize Baudrate */ 209 if (Phoenix_SetBaudrate (DEFAULT_BAUDRATE)) 210 return ERROR; 211 199 212 #ifdef SCI_DEV 200 // Activate ICC 201 if (!Sci_Activate())202 return ICC_ASYNC_IFD_ERROR;203 // Reset ICC 204 205 if (!Sci_Reset(atr)) 206 return ICC_ASYNC_IFD_ERROR;207 208 213 /* Activate ICC */ 214 if (Sci_Activate()) 215 return ERROR; 216 /* Reset ICC */ 217 if (reader[ridx].typ == R_INTERNAL) { 218 if (Sci_Reset(atr)) 219 return ERROR; 220 } 221 else 209 222 #endif 210 223 #ifdef COOL 211 if (reader[ridx].typ == R_INTERNAL) { 212 if (!Cool_Reset(atr)) 213 return ICC_ASYNC_IFD_ERROR; 214 } 215 else 216 #endif 217 if (!Phoenix_Reset(atr)) 218 return ICC_ASYNC_IFD_ERROR; 219 // Get ICC convention 220 if (ATR_GetConvention (atr, &(convention)) != ATR_OK) 221 { 224 if (reader[ridx].typ == R_INTERNAL) { 225 if (Cool_Reset(atr)) 226 return ERROR; 227 } 228 else 229 #endif 230 if (Phoenix_Reset(atr)) { 231 return ERROR; 232 } 233 #if defined(LIBUSB) 234 } 235 #endif 236 237 unsigned char atrarr[64]; 238 unsigned int atr_size; 239 ATR_GetRaw(atr, atrarr, &atr_size); 240 cs_ri_log("ATR: %s", cs_hexdump(1, atrarr, atr_size)); 241 242 /* Get ICC convention */ 243 if (ATR_GetConvention (atr, &(convention)) != ATR_OK) { 222 244 convention = 0; 223 //no protocol_type = -1 ???224 return ICC_ASYNC_ATR_ERROR;245 protocol_type = 0; 246 return ERROR; 225 247 } 226 248 227 249 protocol_type = ATR_PROTOCOL_TYPE_T0; 228 250 229 ATR_GetNumberOfProtocols (atr, &np); 230 231 /* 232 * Get protocol offered by interface bytes T*2 if available, 233 * (that is, if TD1 is available), * otherwise use default T=0 234 */ 235 /* if (np>1) 236 ATR_GetProtocolType (atr, 1, &(protocol_type)); 237 238 #ifdef DEBUG_ICC 239 printf("ICC: Detected %s convention processor card T=%d\n",(convention == ATR_CONVENTION_DIRECT ? "direct" : "inverse"), protocol_type); 240 #endif 241 *///really should let PPS handle this 242 243 /* Initialize member variables */ 244 if (convention == ATR_CONVENTION_INVERSE) 245 { 246 if (!IO_Serial_SetParity (PARITY_ODD)) 247 return ICC_ASYNC_IFD_ERROR; 248 } 249 else if(protocol_type == ATR_PROTOCOL_TYPE_T14) 250 { 251 if (!IO_Serial_SetParity (PARITY_NONE)) 252 return ICC_ASYNC_IFD_ERROR; 253 } 254 else 255 { 256 if (!IO_Serial_SetParity (PARITY_EVEN)) 257 return ICC_ASYNC_IFD_ERROR; 258 } 259 #ifdef COOL 260 if (reader[ridx].typ != R_INTERNAL) 261 #endif 262 IO_Serial_Flush(); 263 return ICC_ASYNC_OK; 264 #else 265 return ICC_ASYNC_ATR_ERROR; 266 #endif 251 if (Parse_ATR(atr)) 252 return ERROR; 253 return OK; 267 254 } 268 255 … … 275 262 276 263 if (reader[ridx].status != 1) //no card inside 277 return ICC_ASYNC_IFD_ERROR;264 return ERROR; 278 265 279 266 /* Create a command APDU */ 280 267 apdu_cmd = APDU_Cmd_New (cmd, lc); 281 268 if (apdu_cmd == NULL) 282 return ICC_ASYNC_IFD_ERROR;283 284 if ( Protocol_Command (apdu_cmd, &apdu_rsp) == ICC_ASYNC_OK) {269 return ERROR; 270 271 if (!Protocol_Command (apdu_cmd, &apdu_rsp)) { 285 272 if (apdu_rsp != NULL) { 286 273 /* Copy APDU data to rsp */ … … 298 285 } 299 286 else 300 return ICC_ASYNC_IFD_ERROR;287 return ERROR; 301 288 302 289 APDU_Cmd_Delete (apdu_cmd); 303 290 if (err) 304 return ICC_ASYNC_IFD_ERROR;305 306 return ICC_ASYNC_OK;291 return ERROR; 292 293 return OK; 307 294 } 308 295 … … 311 298 switch (protocol_type) { 312 299 case ATR_PROTOCOL_TYPE_T0: 313 if (Protocol_T0_Command (cmd, rsp) != ICC_ASYNC_OK)314 return ICC_ASYNC_IFD_ERROR;300 if (Protocol_T0_Command (cmd, rsp)) 301 return ERROR; 315 302 break; 316 303 case ATR_PROTOCOL_TYPE_T1: 317 if (Protocol_T1_Command (cmd, rsp) != ICC_ASYNC_OK) //FIXME in .h and also .c !!!318 return ICC_ASYNC_IFD_ERROR;304 if (Protocol_T1_Command (cmd, rsp)) 305 return ERROR; 319 306 break; 320 307 case ATR_PROTOCOL_TYPE_T14: 321 if (Protocol_T14_Command (cmd, rsp) != ICC_ASYNC_OK)322 return ICC_ASYNC_IFD_ERROR;308 if (Protocol_T14_Command (cmd, rsp)) 309 return ERROR; 323 310 break; 324 311 default: 325 312 cs_log("Error, unknown protocol type %i",protocol_type); 326 return ICC_ASYNC_IFD_ERROR;327 } 328 return ICC_ASYNC_OK;329 } 330 331 int ICC_Async_SetTimings (unsigned short bwt)332 { 333 if (protocol_type == ATR_PROTOCOL_TYPE_T1) {334 if (reader[ridx].typ == R_INTERNAL) { 335 #ifdef SCI_DEV 336 #include <sys/ioctl.h> 337 #include "sci_global.h" 338 #include "sci_ioctl.h"339 SCI_PARAMETERS params; 340 if (ioctl(reader[ridx].handle, IOCTL_GET_PARAMETERS, ¶ms) < 0 ) 341 return ICC_ASYNC_IFD_ERROR; 342 params.BWT = bwt;343 if (ioctl(reader[ridx].handle, IOCTL_SET_PARAMETERS, ¶ms)!=0) 344 return ICC_ASYNC_IFD_ERROR; 345 #endif 346 } 347 else 348 icc_timings.block_timeout = ETU_to_ms(bwt); 349 cs_debug("Setting BWT to %i", bwt);350 BWT = bwt;351 }352 return ICC_ASYNC_OK;313 return ERROR; 314 } 315 return OK; 316 } 317 318 int ICC_Async_SetTimings (unsigned wait_etu) 319 { 320 // if (reader[ridx].typ == R_INTERNAL) { 321 //not sure whether this tampering with Block/Character timeouts is needed with SCI devices, 322 //since we have set CWT and BWT it seems logical the reader is taking care of this 323 //#ifdef SCI_DEV 324 //#include <sys/ioctl.h> 325 //#include "sci_global.h" 326 //#include "sci_ioctl.h" 327 // SCI_PARAMETERS params; 328 // if (ioctl(reader[ridx].handle, IOCTL_GET_PARAMETERS, ¶ms) < 0 ) 329 // return ERROR; 330 // params.BWT = bwt; 331 // if (ioctl(reader[ridx].handle, IOCTL_SET_PARAMETERS, ¶ms)!=0) 332 // return ERROR; 333 //#endif 334 // } 335 // else { 336 read_timeout = ETU_to_ms(wait_etu); 337 //cs_debug("Setting timeout to %i", wait_etu); 338 // } 339 return OK; 353 340 } 354 341 355 342 int ICC_Async_SetBaudrate (unsigned long baudrate) 356 343 { 357 if (!Phoenix_SetBaudrate (baudrate)) 358 return ICC_ASYNC_IFD_ERROR; 359 360 return ICC_ASYNC_OK; 344 if (Phoenix_SetBaudrate (baudrate)) //also call this for internal readers to update current_baudrate 345 return ERROR; 346 347 #ifdef COOL 348 if(reader[ridx].typ == R_INTERNAL) 349 if (Cool_SetBaudrate(reader[ridx].mhz)) 350 return ERROR; 351 #endif 352 353 #if defined(LIBUSB) 354 if (reader[ridx].typ == R_SMART) { 355 if(SR_SetBaudrate(&reader[ridx])) 356 return ERROR; 357 return OK; 358 } 359 #endif 360 361 return OK; 361 362 } 362 363 … … 365 366 BYTE *buffer = NULL, *sent; 366 367 368 #if defined(LIBUSB) 369 if (convention == ATR_CONVENTION_INVERSE && reader[ridx].typ != R_INTERNAL && reader[ridx].typ != R_SMART) 370 #else 367 371 if (convention == ATR_CONVENTION_INVERSE && reader[ridx].typ != R_INTERNAL) 372 #endif 368 373 { 369 374 buffer = (BYTE *) calloc(sizeof (BYTE), size); … … 377 382 } 378 383 384 #if defined(LIBUSB) 385 if (reader[ridx].typ == R_SMART) { 386 if (SR_Transmit(&reader[ridx], sent, size)) 387 return ERROR; 388 } 389 else 390 #endif 391 379 392 #ifdef COOL 393 if (reader[ridx].typ == R_INTERNAL) { 394 if (Cool_Transmit(sent, size)) 395 return ERROR; 396 } 397 else 398 #elif SCI_DEV 380 399 if (reader[ridx].typ == R_INTERNAL) { 381 if (!Cool_Transmit(sent, size)) 382 return ICC_ASYNC_IFD_ERROR; 383 } 384 else 385 #elif SCI_DEV 386 if (reader[ridx].typ == R_INTERNAL) { 387 if (!Phoenix_Transmit (sent, size, 0, 0)) //the internal reader will provide the delay 388 return ICC_ASYNC_IFD_ERROR; 389 } 390 else 391 #endif 392 if (!Phoenix_Transmit (sent, size, icc_timings.block_delay, icc_timings.char_delay)) 393 return ICC_ASYNC_IFD_ERROR; 394 400 if (Phoenix_Transmit (sent, size, 0, 0)) //the internal reader will provide the delay 401 return ERROR; 402 } 403 else 404 #endif 405 if (Phoenix_Transmit (sent, size, icc_timings.block_delay, icc_timings.char_delay)) 406 return ERROR; 407 408 #if defined(LIBUSB) 409 if (convention == ATR_CONVENTION_INVERSE && reader[ridx].typ != R_INTERNAL && reader[ridx].typ != R_SMART) 410 #else 395 411 if (convention == ATR_CONVENTION_INVERSE && reader[ridx].typ != R_INTERNAL) 412 #endif 396 413 free (buffer); 397 398 return ICC_ASYNC_OK;414 415 return OK; 399 416 } 400 417 … … 402 419 { 403 420 #ifdef COOL 404 if (reader[ridx].typ == R_INTERNAL) { 405 if (!Cool_Receive(data, size)) 406 return ICC_ASYNC_IFD_ERROR; 407 } 408 else 421 if (reader[ridx].typ == R_INTERNAL) { 422 if (Cool_Receive(data, size)) 423 return ERROR; 424 } 425 else 426 #endif 427 #if defined(LIBUSB) 428 if (reader[ridx].typ == R_SMART) { 429 if (SR_Receive(&reader[ridx], data, size)) 430 return ERROR; 431 } 432 else 433 #endif 434 435 if (Phoenix_Receive (data, size, read_timeout)) 436 return ERROR; 437 438 #if defined(LIBUSB) 439 if (convention == ATR_CONVENTION_INVERSE && reader[ridx].typ != R_INTERNAL && reader[ridx].typ != R_SMART) 409 440 #else 410 if (!Phoenix_Receive (data, size, icc_timings.block_timeout, icc_timings.char_timeout))411 return ICC_ASYNC_IFD_ERROR;412 #endif413 414 441 if (convention == ATR_CONVENTION_INVERSE && reader[ridx].typ != R_INTERNAL) 442 #endif 415 443 ICC_Async_InvertBuffer (size, data); 416 444 417 return ICC_ASYNC_OK;445 return OK; 418 446 } 419 447 420 448 int ICC_Async_Close () 421 449 { 450 #if defined(LIBUSB) 451 if (reader[ridx].typ == R_SMART) { 452 if (SR_Close(&reader[ridx])) 453 return ERROR; 454 } 455 else 456 #endif 457 422 458 #ifdef SCI_DEV 423 459 /* Dectivate ICC */ 424 if (!Sci_Deactivate()) 425 return ICC_ASYNC_IFD_ERROR; 426 #endif 427 428 /* Delete atr */ 429 convention = 0; 430 protocol_type = -1; 431 432 return ICC_ASYNC_OK; 460 if (Sci_Deactivate()) 461 return ERROR; 462 #endif 463 464 return OK; 433 465 } 434 466 … … 457 489 buffer[i] = ~(INVERT_BYTE (buffer[i])); 458 490 } 491 492 static int Parse_ATR (ATR * atr) 493 { 494 BYTE FI = ATR_DEFAULT_FI; 495 //BYTE t = ATR_PROTOCOL_TYPE_T0; 496 double d = ATR_DEFAULT_D; 497 double n = ATR_DEFAULT_N; 498 int ret; 499 bool PPS_success; 500 501 /* Perform PPS Exchange if requested by command */ 502 PPS_success = OK; 503 if (!PPS_success) 504 { 505 int numprot = atr->pn; 506 //if there is a trailing TD, this number is one too high 507 BYTE tx; 508 if (ATR_GetInterfaceByte (atr, numprot-1, ATR_INTERFACE_BYTE_TD, &tx) == ATR_OK) 509 if ((tx & 0xF0) == 0) 510 numprot--; 511 cs_debug("ATR reports %i protocol lines:",numprot); 512 int i,point; 513 char txt[50]; 514 bool OffersT[3]; //T14 stored as T2 515 for (i = 0; i <= 2; i++) 516 OffersT[i] = FALSE; 517 for (i=1; i<= numprot; i++) { 518 point = 0; 519 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TA, &tx) == ATR_OK) { 520 sprintf((char *)txt+point,"TA%i=%02X ",i,tx); 521 point +=7; 522 } 523 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TB, &tx) == ATR_OK) { 524 sprintf((char *)txt+point,"TB%i=%02X ",i,tx); 525 point +=7; 526 } 527 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TC, &tx) == ATR_OK) { 528 sprintf((char *)txt+point,"TC%i=%02X ",i,tx); 529 point +=7; 530 } 531 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TD, &tx) == ATR_OK) { 532 sprintf((char *)txt+point,"TD%i=%02X ",i,tx); 533 point +=7; 534 tx &= 0X0F; 535 sprintf((char *)txt+point,"(T%i)",tx); 536 if (tx == 14) 537 OffersT[2] = TRUE; 538 else 539 OffersT[tx] = TRUE; 540 } 541 else { 542 sprintf((char *)txt+point,"no TD%i means T0",i); 543 OffersT[0] = TRUE; 544 } 545 cs_debug("%s",txt); 546 } 547 548 int numprottype = 0; 549 for (i = 0; i <= 2; i++) 550 if (OffersT[i]) 551 numprottype ++; 552 cs_debug("%i protocol types detected. Historical bytes: %s",numprottype, cs_hexdump(1,atr->hb,atr->hbn)); 553 554 //If more than one protocol type and/or TA1 parameter values other than the default values and/or N equeal to 255 is/are indicated in the answer to reset, the card shall know unambiguously, after having sent the answer to reset, which protocol type or/and transmission parameter values (FI, D, N) will be used. Consequently a selection of the protocol type and/or the transmission parameters values shall be specified. 555 ATR_GetParameter (atr, ATR_PARAMETER_N, &(n)); 556 ATR_GetProtocolType(atr,1,&(protocol_type)); //get protocol from TD1 557 BYTE TA2; 558 bool SpecificMode = (ATR_GetInterfaceByte (atr, 2, ATR_INTERFACE_BYTE_TA, &TA2) == ATR_OK); //if TA2 present, specific mode, else negotiable mode 559 if (SpecificMode) { 560 protocol_type = TA2 & 0x0F; 561 if ((TA2 & 0x10) != 0x10) { //bit 5 set to 0 means F and D explicitly defined in interface characters 562 BYTE TA1; 563 if (ATR_GetInterfaceByte (atr, 1 , ATR_INTERFACE_BYTE_TA, &TA1) == ATR_OK) { 564 FI = TA1 >> 4; 565 ATR_GetParameter (atr, ATR_PARAMETER_D, &(d)); 566 } 567 else { 568 FI = ATR_DEFAULT_FI; 569 d = ATR_DEFAULT_D; 570 } 571 } 572 else { 573 cs_log("Specific mode: speed 'implicitly defined', not sure how to proceed, assuming default values"); 574 FI = ATR_DEFAULT_FI; 575 d = ATR_DEFAULT_D; 576 } 577 cs_debug("Specific mode: T%i, F=%.0f, D=%.6f, N=%.0f\n", protocol_type, (double) atr_f_table[FI], d, n); 578 } 579 else { //negotiable mode 580 581 bool NeedsPTS = ((protocol_type != ATR_PROTOCOL_TYPE_T14) && (numprottype > 1 || (atr->ib[0][ATR_INTERFACE_BYTE_TA].present == TRUE && atr->ib[0][ATR_INTERFACE_BYTE_TA].value != 0x11) || n == 255)); //needs PTS according to ISO 7816 , SCI gets stuck on our PTS 582 if (NeedsPTS && reader[ridx].deprecated == 0) { 583 // PTSS PTS0 PTS1 PTS2 PTS3 PCK 584 // PTSS PTS0 PTS1 PCK 585 BYTE req[] = { 0xFF, 0x10, 0x00, 0x00 }; //we currently do not support PTS2, standard guardtimes 586 req[1]=0x10 | protocol_type; //PTS0 always flags PTS1 to be sent always 587 if (ATR_GetInterfaceByte (atr, 1, ATR_INTERFACE_BYTE_TA, &req[2]) != ATR_OK) //PTS1 588 req[2] = 0x11; //defaults FI and DI to 1 589 //req[3]=PPS_GetPCK(req,sizeof(req)-1); will be set by PPS_Exchange 590 unsigned int len = sizeof(req); 591 ret = PPS_Exchange (req, &len); 592 if (ret == OK) { 593 FI = req[2] >> 4; 594 BYTE DI = req[2] & 0x0F; 595 d = (double) (atr_d_table[DI]); 596 PPS_success = TRUE; 597 cs_debug("PTS Succesfull, selected protocol: T%i, F=%.0f, D=%.6f, N=%.0f\n", protocol_type, (double) atr_f_table[FI], d, n); 598 } 599 else 600 cs_ddump(req,4,"PTS Failure, response:"); 601 } 602 603 //FIXME Currently InitICC sets baudrate to 9600 for all T14 cards (=no switching); 604 //When for SCI, T14 protocol, TA1 is obeyed, this goes OK for mosts devices, but somehow on DM7025 Sky S02 card goes wrong when setting ETU (ok on DM800/DM8000) 605 if (!PPS_success) {//last PPS not succesfull 606 BYTE TA1; 607 if (ATR_GetInterfaceByte (atr, 1 , ATR_INTERFACE_BYTE_TA, &TA1) == ATR_OK) { 608 FI = TA1 >> 4; 609 ATR_GetParameter (atr, ATR_PARAMETER_D, &(d)); 610 } 611 else { //do not obey TA1 612 FI = ATR_DEFAULT_FI; 613 d = ATR_DEFAULT_D; 614 } 615 if (NeedsPTS) { 616 if ((d == 32) || (d == 12) || (d == 20)) //those values were RFU in old table 617 d = 0; // viaccess cards that fail PTS need this 618 } 619 620 cs_debug("No PTS %s, selected protocol T%i, F=%.0f, D=%.6f, N=%.0f\n", NeedsPTS?"happened":"needed", protocol_type, (double) atr_f_table[FI], d, n); 621 } 622 }//end negotiable mode 623 }//end length<0 624 625 //make sure no zero values 626 double F = (double) atr_f_table[FI]; 627 if (!F) { 628 FI = ATR_DEFAULT_FI; 629 cs_log("Warning: F=0 is invalid, forcing FI=%d", FI); 630 } 631 if (!d) { 632 d = ATR_DEFAULT_D; 633 cs_log("Warning: D=0 is invalid, forcing D=%.0f",d); 634 } 635 636 #ifdef DEBUG_PROTOCOL 637 printf("PPS: T=%i, F=%.0f, D=%.6f, N=%.0f\n", t, F, d, n); 638 #endif 639 640 #if defined(LIBUSB) 641 if (reader[ridx].typ == R_SMART) { 642 reader[ridx].sr_config.F=atr_f_table[FI]; 643 reader[ridx].sr_config.D=d; 644 reader[ridx].sr_config.N=n; 645 reader[ridx].sr_config.T=protocol_type; 646 SR_SetBaudrate(&reader[ridx]); 647 SetRightParity (); 648 return OK; 649 } 650 else { 651 #endif 652 if (reader[ridx].deprecated == 0) 653 return InitCard (atr, FI, d, n); 654 else { 655 cs_log("Warning: entering Deprecated Mode"); 656 return InitCard (atr, ATR_DEFAULT_FI, ATR_DEFAULT_D, n); 657 } 658 #if defined(LIBUSB) 659 } 660 #endif 661 } 662 663 /* 664 * Not exported funtions definition 665 */ 666 667 static int PPS_Exchange (BYTE * params, unsigned *length) 668 { 669 BYTE confirm[PPS_MAX_LENGTH]; 670 unsigned len_request, len_confirm; 671 int ret; 672 #ifdef DEBUG_PROTOCOL 673 unsigned int i; 674 #endif 675 676 len_request = PPS_GetLength (params); 677 params[len_request - 1] = PPS_GetPCK(params, len_request - 1); 678 679 #ifdef DEBUG_PROTOCOL 680 printf ("PPS: Sending request: "); 681 for (i = 0; i < len_request; i++) 682 printf ("%X ", params[i]); 683 printf ("\n"); 684 #endif 685 686 cs_debug("PTS: Sending request: %s", cs_hexdump(1, params, len_request)); 687 688 /* Send PPS request */ 689 #ifdef COOL 690 //unsigned char ptsAck[10]; 691 //u_int8 ptsLen = len_request; 692 unsigned short int ptsLen = len_request; 693 int Status = cnxt_smc_start_pps(handle, params, confirm, &ptsLen, TRUE); 694 printf ("cnxt_smc_start_pps Status=%i\n", Status); 695 len_confirm = ptsLen; 696 #ifdef DEBUG_PROTOCOL 697 printf("COOL: confirm: \n"); 698 for (i = 0; i < ptsLen; i++) 699 printf ("%02X", confirm[i]); 700 printf ("\n"); 701 fflush(stdout); 702 printf("COOL: req: \n"); 703 for (i = 0; i < len_request; i++) 704 printf ("%02X", params[i]); 705 printf ("\n"); 706 fflush(stdout); 707 #endif 708 if (Status) 709 return ERROR; 710 #else 711 if (ICC_Async_Transmit (len_request, params)) 712 return ERROR; 713 714 /* Get PPS confirm */ 715 if (ICC_Async_Receive (2, confirm)) 716 return ERROR; 717 718 len_confirm = PPS_GetLength (confirm); 719 720 if (ICC_Async_Receive (len_confirm - 2, confirm + 2)) 721 return ERROR; 722 723 #ifdef DEBUG_PROTOCOL 724 printf ("PPS: Receivig confirm: "); 725 for (i = 0; i < len_confirm; i++) 726 printf ("%X ", confirm[i]); 727 printf ("\n"); 728 #endif 729 730 cs_debug("PTS: Receiving confirm: %s", cs_hexdump(1, confirm, len_confirm)); 731 732 if ((len_request != len_confirm) || (memcmp (params, confirm, len_request))) 733 ret = ERROR; 734 else 735 ret = OK; 736 #endif 737 738 /* Copy PPS handsake */ 739 memcpy (params, confirm, len_confirm); 740 (*length) = len_confirm; 741 742 return ret; 743 } 744 745 static unsigned PPS_GetLength (BYTE * block) 746 { 747 unsigned length = 3; 748 749 if (PPS_HAS_PPS1 (block)) 750 length++; 751 752 if (PPS_HAS_PPS2 (block)) 753 length++; 754 755 if (PPS_HAS_PPS3 (block)) 756 length++; 757 758 return length; 759 } 760 761 static unsigned int ETU_to_ms(unsigned long WWT) 762 { 763 #define CHAR_LEN 10L //character length in ETU, perhaps should be 9 when parity = none? 764 if (WWT > CHAR_LEN) 765 WWT -= CHAR_LEN; 766 else 767 WWT = 0; 768 double work_etu = 1000 / (double)current_baudrate;//FIXME sometimes work_etu should be used, sometimes initial etu 769 return (unsigned int) WWT * work_etu * reader[ridx].cardmhz / reader[ridx].mhz; 770 } 771 772 static int SetRightParity (void) 773 { 774 //set right parity 775 if (convention == ATR_CONVENTION_INVERSE) 776 { 777 #if defined(LIBUSB) 778 if (reader[ridx].typ == R_SMART) { 779 reader[ridx].sr_config.inv= (convention == ATR_CONVENTION_INVERSE) ? 1: 0; 780 reader[ridx].sr_config.parity=ODD; 781 if(SR_SetParity(&reader[ridx])) 782 return ERROR; 783 } 784 else 785 #endif 786 if (IO_Serial_SetParity (PARITY_ODD)) 787 return ERROR; 788 } 789 else if(protocol_type == ATR_PROTOCOL_TYPE_T14) 790 { 791 #if defined(LIBUSB) 792 if (reader[ridx].typ == R_SMART) { 793 reader[ridx].sr_config.parity=NONE; 794 if(SR_SetParity(&reader[ridx])) 795 return ERROR; 796 } 797 else 798 #endif 799 if (IO_Serial_SetParity (PARITY_NONE)) 800 return ERROR; 801 } 802 else 803 { 804 #if defined(LIBUSB) 805 if (reader[ridx].typ == R_SMART) { 806 reader[ridx].sr_config.parity=EVEN; 807 if(SR_SetParity(&reader[ridx])) 808 return ERROR; 809 } 810 else 811 #endif 812 if (IO_Serial_SetParity (PARITY_EVEN)) 813 return ERROR; 814 } 815 #ifdef COOL 816 if (reader[ridx].typ != R_INTERNAL) 817 #endif 818 #if defined(LIBUSB) 819 if (reader[ridx].typ != R_SMART) 820 #endif 821 IO_Serial_Flush(); 822 return OK; 823 } 824 825 static int InitCard (ATR * atr, BYTE FI, double d, double n) 826 { 827 //set the amps and the volts according to ATR 828 double P,I; 829 if (ATR_GetParameter(atr, ATR_PARAMETER_P, &P) != ATR_OK) 830 P = 0; 831 if (ATR_GetParameter(atr, ATR_PARAMETER_I, &I) != ATR_OK) 832 I = 0; 833 834 //set clock speed to max if internal reader 835 if(reader[ridx].typ == R_INTERNAL) 836 if (reader[ridx].mhz == 357 || reader[ridx].mhz == 358) //no overclocking 837 reader[ridx].mhz = atr_fs_table[FI] / 10000; //we are going to clock the card to this nominal frequency 838 839 //set clock speed/baudrate must be done before timings 840 //because current_baudrate is used in calculation of timings 841 cs_log("Maximum frequency for this card is formally %i Mhz, clocking it to %.2f Mhz", atr_fs_table[FI] / 1000000, (float) reader[ridx].mhz / 100); 842 unsigned long baudrate; 843 double F = (double) atr_f_table[FI]; 844 if (protocol_type == ATR_PROTOCOL_TYPE_T14) 845 baudrate = 9600; 846 else 847 baudrate = d * ICC_Async_GetClockRate () / F; 848 849 #ifdef DEBUG_PROTOCOL 850 printf ("PPS: Baudrate = %d\n", (int)baudrate); 851 #endif 852 853 if (reader[ridx].deprecated == 0) 854 if (ICC_Async_SetBaudrate (baudrate)) 855 return ERROR; 856 857 //set timings according to ATR 858 unsigned long BGT, edc, EGT, CGT, WWT = 0; 859 read_timeout = 0; 860 icc_timings.block_delay = 0; 861 icc_timings.char_delay = 0; 862 863 if (n == 255) //Extra Guard Time 864 EGT = 0; 865 else 866 EGT = n; 867 unsigned int GT = EGT + 12; //Guard Time in ETU 868 unsigned long gt_ms = ETU_to_ms(GT); 869 870 switch (protocol_type) { 871 case ATR_PROTOCOL_TYPE_T0: 872 case ATR_PROTOCOL_TYPE_T14: 873 { 874 BYTE wi; 875 /* Integer value WI = TC2, by default 10 */ 876 #ifndef PROTOCOL_T0_USE_DEFAULT_TIMINGS 877 if (ATR_GetInterfaceByte (atr, 2, ATR_INTERFACE_BYTE_TC, &(wi)) != ATR_OK) 878 #endif 879 wi = DEFAULT_WI; 880 881 // WWT = 960 * WI * (Fi / f) * 1000 milliseconds 882 WWT = (unsigned long) 960 * wi; //in ETU 883 if (protocol_type == ATR_PROTOCOL_TYPE_T14) 884 WWT >>= 1; //is this correct? 885 886 read_timeout = ETU_to_ms(WWT); 887 icc_timings.block_delay = gt_ms; 888 icc_timings.char_delay = gt_ms; 889 cs_debug("Setting timings: timeout=%u ms, block_delay=%u ms, char_delay=%u ms", read_timeout, icc_timings.block_delay, icc_timings.char_delay); 890 #ifdef DEBUG_PROTOCOL 891 printf ("Protocol: T=%i: WWT=%d, Clockrate=%lu\n", t, (int)(WWT), ICC_Async_GetClockRate()); 892 #endif 893 } 894 break; 895 case ATR_PROTOCOL_TYPE_T1: 896 { 897 BYTE ta, tb, tc, cwi, bwi; 898 899 // Set IFSC 900 if (ATR_GetInterfaceByte (atr, 3, ATR_INTERFACE_BYTE_TA, &ta) == ATR_NOT_FOUND) 901 ifsc = DEFAULT_IFSC; 902 else if ((ta != 0x00) && (ta != 0xFF)) 903 ifsc = ta; 904 else 905 ifsc = DEFAULT_IFSC; 906 907 // Towitoko does not allow IFSC > 251 //FIXME not sure whether this limitation still exists 908 ifsc = MIN (ifsc, MAX_IFSC); 909 910 #ifndef PROTOCOL_T1_USE_DEFAULT_TIMINGS 911 // Calculate CWI and BWI 912 if (ATR_GetInterfaceByte (atr, 3, ATR_INTERFACE_BYTE_TB, &tb) == ATR_NOT_FOUND) 913 { 914 #endif 915 cwi = DEFAULT_CWI; 916 bwi = DEFAULT_BWI; 917 #ifndef PROTOCOL_T1_USE_DEFAULT_TIMINGS 918 } 919 else 920 { 921 cwi = tb & 0x0F; 922 bwi = tb >> 4; 923 } 924 #endif 925 926 // Set CWT = (2^CWI + 11) work etu 927 CWT = (unsigned short) (((1<<cwi) + 11)); // in ETU 928 929 // Set BWT = (2^BWI * 960 + 11) work etu 930 BWT = (unsigned short)((1<<bwi) * 960 * 372 * 9600 / ICC_Async_GetClockRate() ) + 11 ; 931 932 // Set BGT = 22 * work etu 933 BGT = 22L; //in ETU 934 935 if (n == 255) 936 CGT = 11L; //in ETU 937 else 938 CGT = GT; 939 940 // Set the error detection code type 941 if (ATR_GetInterfaceByte (atr, 3, ATR_INTERFACE_BYTE_TC, &tc) == ATR_NOT_FOUND) 942 edc = EDC_LRC; 943 else 944 edc = tc & 0x01; 945 946 // Set initial send sequence (NS) 947 ns = 1; 948 949 cs_debug ("Protocol: T=1: IFSC=%d, CWT=%d etu, BWT=%d etu, BGT=%d etu, EDC=%s\n", ifsc, CWT, BWT, BGT, (edc == EDC_LRC) ? "LRC" : "CRC"); 950 951 read_timeout = ETU_to_ms(BWT); 952 icc_timings.block_delay = ETU_to_ms(BGT); 953 icc_timings.char_delay = ETU_to_ms(CGT); 954 cs_debug("Setting timings: timeout=%u ms, block_delay=%u ms, char_delay=%u ms", read_timeout, icc_timings.block_delay, icc_timings.char_delay); 955 } 956 break; 957 default: 958 return ERROR; 959 break; 960 }//switch 961 962 if (SetRightParity ()) 963 return ERROR; 964 965 //write settings to internal device 966 if(reader[ridx].typ == R_INTERNAL) { 967 #ifdef SCI_DEV 968 double F = (double) atr_f_table[FI]; 969 unsigned long ETU = 0; 970 //for Irdeto T14 cards, do not set ETU 971 if (!(atr->hbn >= 6 && !memcmp(atr->hb, "IRDETO", 6) && protocol_type == 14)) 972 ETU = F / d; 973 if (Sci_WriteSettings (protocol_type, reader[ridx].mhz / 100, ETU, WWT, BWT, CWT, EGT, (unsigned char)P, (unsigned char)I)) 974 return ERROR; 975 #elif COOL 976 if (Cool_WriteSettings (BWT, CWT, EGT, BGT)) 977 return ERROR; 978 #endif //COOL 979 } 980 981 //IFS setting in case of T1 982 if ((protocol_type == ATR_PROTOCOL_TYPE_T1) && (ifsc != DEFAULT_IFSC)) { 983 APDU_Cmd * cmd; 984 APDU_Rsp ** rsp; 985 unsigned char tmp[] = { 0x21, 0xC1, 0x01, 0x00, 0x00 }; 986 tmp[3] = ifsc; // Information Field size 987 tmp[4] = ifsc ^ 0xE1; 988 cmd = APDU_Cmd_New (tmp, 5L); 989 Protocol_T1_Command (cmd, rsp); 990 APDU_Cmd_Delete (cmd); 991 } 992 993 return OK; 994 } 995 996 static BYTE PPS_GetPCK (BYTE * block, unsigned length) 997 { 998 BYTE pck; 999 unsigned i; 1000 1001 pck = block[0]; 1002 for (i = 1; i < length; i++) 1003 pck ^= block[i]; 1004 1005 return pck; 1006 }
Note:
See TracChangeset
for help on using the changeset viewer.