source: trunk/csctapi/pps.c@ 1278

Last change on this file since 1278 was 1278, checked in by dingo35, 11 years ago

Cleanup ICC_Async: atr, baudrate, timings, convention

File size: 16.2 KB
Line 
1/*
2 pps.c
3 Protocol Parameters Selection
4
5 This file is part of the Unix driver for Towitoko smartcard readers
6 Copyright (C) 2000 2001 Carlos Prados <cprados@yahoo.com>
7
8 This version is modified by doz21 to work in a special manner ;)
9
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
14
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23*/
24
25#include "pps.h"
26#include "atr.h"
27#include "protocol_t0.h"
28#include "protocol_t1.h"
29#include <stdlib.h>
30#include <stdio.h>
31#include <string.h>
32#include "ifd.h"
33#include "../globals.h"
34
35/*
36 * Not exported constants definition
37 */
38
39#define PPS_DEFAULT_PROTOCOL 0x00
40
41/*
42 * Not exported macros definition
43 */
44
45#define PPS_HAS_PPS1(block) ((block[1] & 0x10) == 0x10)
46#define PPS_HAS_PPS2(block) ((block[1] & 0x20) == 0x20)
47#define PPS_HAS_PPS3(block) ((block[1] & 0x40) == 0x40)
48
49/*
50 * Not exported funtions declaration
51 */
52
53static int PPS_Exchange (PPS * pps, BYTE * params, unsigned *length);
54
55static bool PPS_Match (BYTE * request, unsigned len_request, BYTE * reply, unsigned len_reply);
56
57static unsigned PPS_GetLength (BYTE * block);
58
59static int PPS_InitICC (PPS * pps);
60
61static int PPS_InitProtocol (PPS * pps, int protocol_selected);
62
63static BYTE PPS_GetPCK (BYTE * block, unsigned length);
64
65/*
66 * Exported functions definition
67 */
68
69PPS * PPS_New (ICC_Async * icc)
70{
71 PPS *pps;
72
73 pps = (PPS *) malloc (sizeof (PPS));
74
75 if (pps != NULL)
76 {
77 pps->icc = icc;
78 pps->protocol = NULL;
79 pps->parameters.t = PPS_DEFAULT_PROTOCOL;
80 pps->parameters.FI = ATR_DEFAULT_FI;
81 pps->parameters.d = ATR_DEFAULT_D;
82 pps->parameters.n = ATR_DEFAULT_N;
83 }
84
85 return pps;
86}
87
88int PPS_Perform (PPS * pps, BYTE * params, unsigned *length)
89{
90 //Performs PPS Exchange on command when *length >0
91 //If unsuccesfull, or when length <= 0
92 //Gets parametes from ATR
93 //If necessary perform PPS session
94 //
95 //Output is pps->params.FI,n,d,t set with correct values
96 //and switched SC-device conform these values to correct baudrate
97 //
98 //We need to store FI instread of F, because SCI_DEV works with FI
99 //and it is easier to overclock then
100 //also from FI -> F is easy, other way around not
101
102 int ret;
103 bool PPS_success;
104
105 /* Perform PPS Exchange if requested by command */
106 if ((*length) > 0)
107 {
108 ret = PPS_Exchange (pps, params, length);
109
110 /* Get parameters from PPS handsake */
111 if (ret == PPS_OK)
112 {
113 pps->parameters.t = params[1] & 0x0F;
114
115 if (PPS_HAS_PPS1 (params))
116 {
117 pps->parameters.FI = (params[2] >> 4);
118 pps->parameters.d = atr_d_table[(params[2] & 0x0F)];
119 }
120
121/*
122 ret = PPS_InitICC(pps);
123
124 if (ret != PPS_OK)
125 return ret;
126*/
127 }
128/*
129 else
130 {
131 return ret;
132 }
133*/
134 }
135 PPS_success = PPS_OK;
136 int protocol_selected = 0; //stores which TAi,TBi etc. bytes must be used 0 means not set
137 if ((*length) <= 0 || !PPS_success) // If not by command, or PPS Exchange by command failed: Try PPS Exchange by ATR or Get parameters from ATR
138 {
139 int numprot = atr->pn;
140 //if there is a trailing TD, this number is one too high
141 BYTE tx;
142 if (ATR_GetInterfaceByte (atr, numprot-1, ATR_INTERFACE_BYTE_TD, &tx) == ATR_OK)
143 if ((tx & 0xF0) == 0)
144 numprot--;
145 cs_debug("ATR reports %i protocol lines:",numprot);
146 int i,point;
147 char txt[50];
148 bool OffersT[3]; //T14 stored as T2
149 for (i = 0; i <= 2; i++)
150 OffersT[i] = FALSE;
151 for (i=1; i<= numprot; i++) {
152 point = 0;
153 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TA, &tx) == ATR_OK) {
154 sprintf((char *)txt+point,"TA%i=%02X ",i,tx);
155 point +=7;
156 }
157 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TB, &tx) == ATR_OK) {
158 sprintf((char *)txt+point,"TB%i=%02X ",i,tx);
159 point +=7;
160 }
161 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TC, &tx) == ATR_OK) {
162 sprintf((char *)txt+point,"TC%i=%02X ",i,tx);
163 point +=7;
164 }
165 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TD, &tx) == ATR_OK) {
166 sprintf((char *)txt+point,"TD%i=%02X ",i,tx);
167 point +=7;
168 tx &= 0X0F;
169 sprintf((char *)txt+point,"(T%i)",tx);
170 if (tx == 14)
171 OffersT[2] = TRUE;
172 else
173 OffersT[tx] = TRUE;
174 }
175 else {
176 sprintf((char *)txt+point,"no TD%i means T0",i);
177 OffersT[0] = TRUE;
178 }
179 cs_debug("%s",txt);
180 }
181
182 int numprottype = 0;
183 for (i = 0; i <= 2; i++)
184 if (OffersT[i])
185 numprottype ++;
186 cs_debug("%i protocol types detected. Historical bytes: %s",numprottype, cs_hexdump(1,atr->hb,atr->hbn));
187
188//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.
189 ATR_GetParameter (atr, ATR_PARAMETER_N, &(pps->parameters.n));
190 ATR_GetProtocolType(atr,1,&(pps->parameters.t)); //get protocol from TD1
191 BYTE TA2;
192 bool SpecificMode = (ATR_GetInterfaceByte (atr, 2, ATR_INTERFACE_BYTE_TA, &TA2) == ATR_OK); //if TA2 present, specific mode, else negotiable mode
193 if (SpecificMode) {
194 pps->parameters.t = TA2 & 0x0F;
195 if ((TA2 & 0x10) != 0x10) { //bit 5 set to 0 means F and D explicitly defined in interface characters
196 BYTE TA1;
197 if (ATR_GetInterfaceByte (atr, 1 , ATR_INTERFACE_BYTE_TA, &TA1) == ATR_OK) {
198 pps->parameters.FI = TA1 >> 4;
199 ATR_GetParameter (atr, ATR_PARAMETER_D, &(pps->parameters.d));
200 }
201 else {
202 pps->parameters.FI = ATR_DEFAULT_FI;
203 pps->parameters.d = ATR_DEFAULT_D;
204 }
205 }
206 else {
207 cs_log("Specific mode: speed 'implicitly defined', not sure how to proceed, assuming default values");
208 pps->parameters.FI = ATR_DEFAULT_FI;
209 pps->parameters.d = ATR_DEFAULT_D;
210 }
211 cs_debug("Specific mode: T%i, F=%.0f, D=%.6f, N=%.0f\n", pps->parameters.t, (double) atr_f_table[pps->parameters.FI], pps->parameters.d, pps->parameters.n);
212 }
213 else { //negotiable mode
214
215 bool NeedsPTS = ((pps->parameters.t != 14) && (numprottype > 1 || (atr->ib[0][ATR_INTERFACE_BYTE_TA].present == TRUE && atr->ib[0][ATR_INTERFACE_BYTE_TA].value != 0x11) || pps->parameters.n == 255)); //needs PTS according to ISO 7816 , SCI gets stuck on our PTS
216 if (NeedsPTS) {
217 // PTSS PTS0 PTS1 PTS2 PTS3 PCK
218 // PTSS PTS0 PTS1 PCK
219 BYTE req[] = { 0xFF, 0x10, 0x00, 0x00 }; //we currently do not support PTS2, standard guardtimes
220 req[1]=0x10 | pps->parameters.t; //PTS0 always flags PTS1 to be sent always
221 if (ATR_GetInterfaceByte (atr, 1, ATR_INTERFACE_BYTE_TA, &req[2]) != ATR_OK) //PTS1
222 req[2] = 0x11; //defaults FI and DI to 1
223 //req[3]=PPS_GetPCK(req,sizeof(req)-1); will be set by PPS_Exchange
224 unsigned int len = sizeof(req);
225 ret = PPS_Exchange (pps, req, &len);
226 if (ret == PPS_OK) {
227 pps->parameters.FI = req[2] >> 4;
228 BYTE DI = req[2] & 0x0F;
229 pps->parameters.d = (double) (atr_d_table[DI]);
230 PPS_success = TRUE;
231 cs_debug("PTS Succesfull, selected protocol: T%i, F=%.0f, D=%.6f, N=%.0f\n", pps->parameters.t, (double) atr_f_table[pps->parameters.FI], pps->parameters.d, pps->parameters.n);
232 }
233 else
234 cs_ddump(req,4,"PTS Failure, response:");
235 }
236
237 //FIXME Currently InitICC sets baudrate to 9600 for all T14 cards (=no switching);
238 //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)
239 if (!PPS_success) {//last PPS not succesfull
240 BYTE TA1;
241 if (ATR_GetInterfaceByte (atr, 1 , ATR_INTERFACE_BYTE_TA, &TA1) == ATR_OK) {
242 pps->parameters.FI = TA1 >> 4;
243 ATR_GetParameter (atr, ATR_PARAMETER_D, &(pps->parameters.d));
244 }
245 else { //do not obey TA1
246 pps->parameters.FI = ATR_DEFAULT_FI;
247 pps->parameters.d = ATR_DEFAULT_D;
248 }
249 ATR_GetProtocolType (atr, 1, &(pps->parameters.t));
250 protocol_selected = 1;
251
252 if (NeedsPTS) {
253 if ((pps->parameters.d == 32) || (pps->parameters.d == 12) || (pps->parameters.d == 20))
254 pps->parameters.d = 0; //behave conform "old" atr_d_table; viaccess cards that fail PTS need this
255 }
256 /////Here all non-ISO behaviour
257 /////End all non-ISO behaviour
258
259 cs_debug("No PTS %s, selected protocol T%i, F=%.0f, D=%.6f, N=%.0f\n", NeedsPTS?"happened":"needed", pps->parameters.t, (double) atr_f_table[pps->parameters.FI], pps->parameters.d, pps->parameters.n);
260 }
261 }//end negotiable mode
262 }//end length<0
263
264 //make sure no zero values
265 double F = (double) atr_f_table[pps->parameters.FI];
266 if (!F) {
267 pps->parameters.FI = ATR_DEFAULT_FI;
268 cs_log("Warning: F=0 is invalid, forcing FI=%d", pps->parameters.FI);
269 }
270 if (!pps->parameters.d) {
271 pps->parameters.d = ATR_DEFAULT_D;
272 cs_log("Warning: D=0 is invalid, forcing D=%.0f",pps->parameters.d);
273 }
274
275 protocol_type = pps->parameters.t;
276
277#ifdef DEBUG_PROTOCOL
278 printf("PPS: T=%i, F=%.0f, D=%.6f, N=%.0f\n",
279 pps->parameters.t,
280 F,
281 pps->parameters.d,
282 pps->parameters.n);
283#endif
284
285 ret = PPS_InitICC(pps);
286
287 if (ret != PPS_OK)
288 return ret;
289
290 /* Initialize selected protocol with selected parameters */
291 if (pps->parameters.t == 1)
292 ret = PPS_InitProtocol (pps, 3); //FIXME in practice most T1 cards carry timing parameters in TA3, TB3 and TC3
293 else
294 ret = PPS_InitProtocol (pps, 2); //FIXME T0 cards carry timing parameters in TC2
295
296 return ret;
297}
298
299void * PPS_GetProtocol (PPS * pps)
300{
301 return pps->protocol;
302}
303
304PPS_ProtocolParameters *PPS_GetProtocolParameters (PPS * pps)
305{
306 /* User must Remember not to reference this struct after removing PPS */
307 return &(pps->parameters);
308}
309
310void PPS_Delete (PPS * pps)
311{
312 free (pps);
313}
314
315/*
316 * Not exported funtions definition
317 */
318
319static int PPS_Exchange (PPS * pps, BYTE * params, unsigned *length)
320{
321 BYTE confirm[PPS_MAX_LENGTH];
322 unsigned len_request, len_confirm;
323 int ret;
324#ifdef DEBUG_PROTOCOL
325 int i;
326#endif
327
328 len_request = PPS_GetLength (params);
329 params[len_request - 1] = PPS_GetPCK(params, len_request - 1);
330
331#ifdef DEBUG_PROTOCOL
332 printf ("PPS: Sending request: ");
333 for (i = 0; i < len_request; i++)
334 printf ("%X ", params[i]);
335 printf ("\n");
336#endif
337
338 cs_debug("PTS: Sending request: %s", cs_hexdump(1, params, len_request));
339
340 /* Send PPS request */
341#ifdef COOL
342 //unsigned char ptsAck[10];
343 //u_int8 ptsLen = len_request;
344 unsigned short int ptsLen = len_request;
345 int Status = cnxt_smc_start_pps(handle, params, confirm, &ptsLen, TRUE);
346 printf ("cnxt_smc_start_pps Status=%i\n", Status);
347 len_confirm = ptsLen;
348#ifdef DEBUG_PROTOCOL
349 printf("COOL: confirm: \n");
350 for (i = 0; i < ptsLen; i++)
351 printf ("%02X", confirm[i]);
352 printf ("\n");
353 fflush(stdout);
354 printf("COOL: req: \n");
355 for (i = 0; i < len_request; i++)
356 printf ("%02X", params[i]);
357 printf ("\n");
358 fflush(stdout);
359#endif
360 if (Status)
361 return PPS_HANDSAKE_ERROR;
362#else
363 if (ICC_Async_Transmit (len_request, params) != ICC_ASYNC_OK)
364 return PPS_ICC_ERROR;
365
366 /* Get PPS confirm */
367 if (ICC_Async_Receive (2, confirm) != ICC_ASYNC_OK)
368 return PPS_ICC_ERROR;
369
370 len_confirm = PPS_GetLength (confirm);
371
372 if (ICC_Async_Receive (len_confirm - 2, confirm + 2) != ICC_ASYNC_OK)
373 return PPS_ICC_ERROR;
374
375#ifdef DEBUG_PROTOCOL
376 printf ("PPS: Receivig confirm: ");
377 for (i = 0; i < len_confirm; i++)
378 printf ("%X ", confirm[i]);
379 printf ("\n");
380#endif
381
382 cs_debug("PTS: Receiving confirm: %s", cs_hexdump(1, confirm, len_confirm));
383
384 if (!PPS_Match (params, len_request, confirm, len_confirm))
385 ret = PPS_HANDSAKE_ERROR;
386 else
387 ret = PPS_OK;
388#endif
389
390 /* Copy PPS handsake */
391 memcpy (params, confirm, len_confirm);
392 (*length) = len_confirm;
393
394 return ret;
395}
396
397static bool PPS_Match (BYTE * request, unsigned len_request, BYTE * confirm, unsigned len_confirm)
398{
399 /* See if the reply differs from request */
400 if ((len_request != len_confirm) || (memcmp (request, confirm, len_request)))
401 {
402 /* See if the card specifies other than default FI and D */
403 //if ((PPS_HAS_PPS1 (confirm)) && (confirm[2] != request[2]))
404 return FALSE;
405 }
406
407 return TRUE;
408}
409
410static unsigned PPS_GetLength (BYTE * block)
411{
412 unsigned length = 3;
413
414 if (PPS_HAS_PPS1 (block))
415 length++;
416
417 if (PPS_HAS_PPS2 (block))
418 length++;
419
420 if (PPS_HAS_PPS3 (block))
421 length++;
422
423 return length;
424}
425
426static int PPS_InitICC (PPS * pps)
427{
428#ifdef SCI_DEV
429#include <sys/ioctl.h>
430#include "sci_global.h"
431#include "sci_ioctl.h"
432 if(reader[ridx].typ == R_INTERNAL)
433 {
434 int n;
435 SCI_PARAMETERS params;
436 //memset(&params,0,sizeof(SCI_PARAMETERS));
437 if (ioctl(reader[ridx].handle, IOCTL_GET_PARAMETERS, &params) < 0 )
438 return PPS_ICC_ERROR;
439
440 params.T = pps->parameters.t;
441 params.fs = atr_fs_table[pps->parameters.FI] / 1000000;
442 double F = (double) atr_f_table[pps->parameters.FI];
443 //for Irdeto T14 cards, do not set ETU
444 if (!(atr->hbn >= 6 && !memcmp(atr->hb, "IRDETO", 6) && params.T == 14))
445 params.ETU = F / pps->parameters.d;
446 if (pps->parameters.n == 255) //only for T0 or also for T1?
447 params.EGT = 0;
448 else
449 params.EGT = pps->parameters.n;
450
451 double a;
452 ATR_GetParameter(atr, ATR_PARAMETER_P, &a);
453 params.P=(unsigned char)a;
454 ATR_GetParameter(atr, ATR_PARAMETER_I, &a);
455 params.I=(unsigned char)a;
456
457 cs_debug("Setting T=%d fs=%lu mhz ETU=%d WWT=%d CWT=%d BWT=%d EGT=%d clock=%d check=%d P=%d I=%d U=%d", (int)params.T, params.fs, (int)params.ETU, (int)params.WWT, (int)params.CWT, (int)params.BWT, (int)params.EGT, (int)params.clock_stop_polarity, (int)params.check, (int)params.P, (int)params.I, (int)params.U);
458
459 if (ioctl(reader[ridx].handle, IOCTL_SET_PARAMETERS, &params)!=0)
460 return PPS_ICC_ERROR;
461
462 }
463#elif COOL
464 if(reader[ridx].typ == R_INTERNAL) {
465 int mhz = atr_fs_table[pps->parameters.FI] / 10000;
466 if (!Cool_SetBaudrate(mhz))
467 return PPS_ICC_ERROR;
468#ifdef DEBUG_PROTOCOL
469 printf("Coolstream: set clock to %i * 10kHz\n", mhz);
470#endif
471 return PPS_OK;
472 }
473#endif
474 {
475 unsigned long baudrate;
476 double F = (double) atr_f_table[pps->parameters.FI];
477 if (pps->parameters.t == 14)
478 baudrate = 9600;
479 else
480 baudrate = pps->parameters.d * ICC_Async_GetClockRate () / F;
481
482#ifdef DEBUG_PROTOCOL
483 printf ("PPS: Baudrate = %d\n", (int)baudrate);
484#endif
485
486
487 if (ICC_Async_SetBaudrate (pps->icc, baudrate) != ICC_ASYNC_OK)
488 return PPS_ICC_ERROR;
489
490 return PPS_OK;
491 }
492}
493
494static int PPS_InitProtocol (PPS * pps, int selected_protocol)
495{
496 int ret;
497
498 if (pps->parameters.t == ATR_PROTOCOL_TYPE_T0)
499 {
500 pps->protocol = Protocol_T0_New ();
501
502 if ((pps->protocol) != NULL)
503 {
504 ret = Protocol_T0_Init ((Protocol_T0 *) pps->protocol, (ICC_Async *) pps->icc, &(pps->parameters), selected_protocol);
505
506 if (ret != PROTOCOL_T0_OK)
507 {
508 Protocol_T0_Delete ((Protocol_T0 *) pps->protocol);
509 pps->protocol = NULL;
510 return PPS_PROTOCOL_ERROR;
511 }
512
513 return PPS_OK;
514 }
515 }
516 else if (pps->parameters.t == ATR_PROTOCOL_TYPE_T1)
517 {
518 pps->protocol = Protocol_T1_New ();
519
520 if (pps->protocol != NULL)
521 {
522 ret = Protocol_T1_Init ((Protocol_T1 *) pps->protocol, (ICC_Async *) pps->icc, selected_protocol);
523
524 if (ret != PROTOCOL_T1_OK)
525 {
526 Protocol_T1_Delete ((Protocol_T1 *) pps->protocol);
527 pps->protocol = NULL;
528 return PPS_PROTOCOL_ERROR;
529 }
530
531 return PPS_OK;
532 }
533 }
534 else if (pps->parameters.t == ATR_PROTOCOL_TYPE_T14)
535 {
536 pps->protocol = Protocol_T14_New ();
537
538 if ((pps->protocol) != NULL)
539 {
540 ret = Protocol_T14_Init ((Protocol_T14 *) pps->protocol, (ICC_Async *) pps->icc, &(pps->parameters), selected_protocol);
541
542 if (ret != PROTOCOL_T14_OK)
543 {
544 Protocol_T14_Delete ((Protocol_T14 *) pps->protocol);
545 pps->protocol = NULL;
546 return PPS_PROTOCOL_ERROR;
547 }
548
549 return PPS_OK;
550 }
551 }
552 else
553 {
554 pps->protocol = NULL;
555 }
556
557 return PPS_PROTOCOL_ERROR;
558}
559
560static BYTE PPS_GetPCK (BYTE * block, unsigned length)
561{
562 BYTE pck;
563 unsigned i;
564
565 pck = block[0];
566 for (i = 1; i < length; i++)
567 pck ^= block[i];
568
569 return pck;
570}
Note: See TracBrowser for help on using the repository browser.