1 | /*
|
---|
2 | icc_async.c
|
---|
3 | Asynchronous ICC's handling functions
|
---|
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 <stdlib.h>
|
---|
26 | #include <stdio.h>
|
---|
27 | #include <string.h>
|
---|
28 | #include "../globals.h"
|
---|
29 | #include "defines.h"
|
---|
30 | #include "icc_async.h"
|
---|
31 | #include "ifd.h"
|
---|
32 | #include "mc_global.h"
|
---|
33 | #include "apdu.h"
|
---|
34 | #include "protocol_t0.h"
|
---|
35 | #include "protocol_t1.h"
|
---|
36 |
|
---|
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 |
|
---|
51 |
|
---|
52 | /*
|
---|
53 | * Not exported functions declaration
|
---|
54 | */
|
---|
55 |
|
---|
56 | static void ICC_Async_InvertBuffer (unsigned size, BYTE * buffer);
|
---|
57 | static int Parse_ATR (ATR * atr, unsigned short deprecated);
|
---|
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, unsigned short deprecated);
|
---|
61 | static unsigned int ETU_to_ms(unsigned long WWT);
|
---|
62 | static BYTE PPS_GetPCK (BYTE * block, unsigned length);
|
---|
63 | static int Protocol_Command (unsigned char * command, unsigned long command_len, APDU_Rsp ** rsp);
|
---|
64 | static int SetRightParity (void);
|
---|
65 |
|
---|
66 | int fdmc=(-1);
|
---|
67 |
|
---|
68 | /*
|
---|
69 | * Exported functions definition
|
---|
70 | */
|
---|
71 |
|
---|
72 | int ICC_Async_Device_Init ()
|
---|
73 | {
|
---|
74 | cs_debug_mask (D_IFD, "IFD: Opening device %s\n", reader[ridx].device);
|
---|
75 |
|
---|
76 | wr = 0;
|
---|
77 | if (reader[ridx].typ <= R_MOUSE)
|
---|
78 | reader[ridx].handle = open (reader[ridx].device, O_RDWR | O_NOCTTY| O_NONBLOCK);
|
---|
79 | if (reader[ridx].handle < 0) {
|
---|
80 | cs_log("ERROR opening device %s",reader[ridx].device);
|
---|
81 | return ERROR;
|
---|
82 | }
|
---|
83 |
|
---|
84 | switch(reader[ridx].typ) {
|
---|
85 | case R_MOUSE:
|
---|
86 | break;
|
---|
87 | #if defined(TUXBOX) && defined(PPC)
|
---|
88 | case R_DB2COM1:
|
---|
89 | case R_DB2COM2:
|
---|
90 | if ((fdmc = open(DEV_MULTICAM, O_RDWR)) < 0) {
|
---|
91 | close(reader[ridx].handle);
|
---|
92 | cs_log("ERROR opening device %s",DEV_MULTICAM);
|
---|
93 | return ERROR;
|
---|
94 | }
|
---|
95 | break;
|
---|
96 | #endif
|
---|
97 | case R_SMART:
|
---|
98 | #if defined(LIBUSB)
|
---|
99 | call (SR_Init(&reader[ridx]));
|
---|
100 | break;
|
---|
101 | #else
|
---|
102 | cs_log("ERROR, you have specified 'protocol = smartreader' in oscam.server,");
|
---|
103 | cs_log("recompile with SmartReader support.");
|
---|
104 | return ERROR;
|
---|
105 | #endif
|
---|
106 | case R_INTERNAL:
|
---|
107 | #ifdef COOL
|
---|
108 | return Cool_Init();
|
---|
109 | #elif SCI_DEV
|
---|
110 | #if defined(SH4) || defined(STB04SCI)
|
---|
111 | reader[ridx].handle = open (reader[ridx].device, O_RDWR|O_NONBLOCK|O_NOCTTY);
|
---|
112 | #else
|
---|
113 | reader[ridx].handle = open (reader[ridx].device, O_RDWR);
|
---|
114 | #endif
|
---|
115 | if (reader[ridx].handle < 0) {
|
---|
116 | cs_log("ERROR opening device %s",reader[ridx].device);
|
---|
117 | return ERROR;
|
---|
118 | }
|
---|
119 | #else//SCI_DEV
|
---|
120 | cs_log("ERROR, you have specified 'protocol = internal' in oscam.server,");
|
---|
121 | cs_log("recompile with internal reader support.");
|
---|
122 | return ERROR;
|
---|
123 | #endif//SCI_DEV
|
---|
124 | break;
|
---|
125 | default:
|
---|
126 | cs_log("ERROR ICC_Device_Init: unknow reader type %i",reader[ridx].typ);
|
---|
127 | return ERROR;
|
---|
128 | }
|
---|
129 |
|
---|
130 | if (reader[ridx].typ <= R_MOUSE)
|
---|
131 | if (Phoenix_Init()) {
|
---|
132 | cs_log("ERROR: Phoenix_Init returns error");
|
---|
133 | Phoenix_Close ();
|
---|
134 | return ERROR;
|
---|
135 | }
|
---|
136 | cs_debug_mask (D_IFD, "IFD: Device %s succesfully opened\n", reader[ridx].device);
|
---|
137 | return OK;
|
---|
138 | }
|
---|
139 |
|
---|
140 | int ICC_Async_GetStatus (int * card)
|
---|
141 | {
|
---|
142 | int in;
|
---|
143 |
|
---|
144 | // printf("\n%08X\n", (int)ifd->io);
|
---|
145 |
|
---|
146 | switch(reader[ridx].typ) {
|
---|
147 | case R_DB2COM1:
|
---|
148 | case R_DB2COM2:
|
---|
149 | #if defined(TUXBOX) && defined(PPC)
|
---|
150 | {
|
---|
151 | ushort msr=1;
|
---|
152 | extern int fdmc;
|
---|
153 | IO_Serial_Ioctl_Lock(1);
|
---|
154 | ioctl(fdmc, GET_PCDAT, &msr);
|
---|
155 | if (reader[ridx].typ == R_DB2COM2)
|
---|
156 | in=(!(msr & 1));
|
---|
157 | else
|
---|
158 | in=((msr & 0x0f00) == 0x0f00);
|
---|
159 | IO_Serial_Ioctl_Lock(0);
|
---|
160 | }
|
---|
161 | break;
|
---|
162 | #endif
|
---|
163 | case R_MOUSE:
|
---|
164 | call (Phoenix_GetStatus(&in));
|
---|
165 | break;
|
---|
166 | #if defined(LIBUSB)
|
---|
167 | case R_SMART:
|
---|
168 | call (SR_GetStatus(&reader[ridx],&in));
|
---|
169 | break;
|
---|
170 | #endif
|
---|
171 | case R_INTERNAL:
|
---|
172 | #ifdef SCI_DEV
|
---|
173 | call (Sci_GetStatus(reader[ridx].handle, &in));
|
---|
174 | #elif COOL
|
---|
175 | call (Cool_GetStatus(&in));
|
---|
176 | #endif
|
---|
177 | break;
|
---|
178 | default:
|
---|
179 | cs_log("ERROR ICC_Device_Init: unknow reader type %i",reader[ridx].typ);
|
---|
180 | return ERROR;
|
---|
181 | }
|
---|
182 |
|
---|
183 | if (in)
|
---|
184 | *card = TRUE;
|
---|
185 | else
|
---|
186 | *card = FALSE;
|
---|
187 |
|
---|
188 | //cs_debug_mask (D_TRACE, "IFD: Status = %s", in ? "card": "no card");
|
---|
189 |
|
---|
190 | return OK;
|
---|
191 | }
|
---|
192 |
|
---|
193 | int ICC_Async_Activate (ATR * atr, unsigned short deprecated)
|
---|
194 | {
|
---|
195 | cs_debug_mask (D_IFD, "IFD: Activating card in reader %s\n", reader[ridx].label);
|
---|
196 | int card;
|
---|
197 |
|
---|
198 | call (ICC_Async_GetStatus (&card));
|
---|
199 | if (card == 0) {
|
---|
200 | cs_log("ERROR: Trying to activate card but no card inside");
|
---|
201 | return ERROR;
|
---|
202 | }
|
---|
203 |
|
---|
204 | current_baudrate = DEFAULT_BAUDRATE; //this is needed for all readers to calculate work_etu for timings
|
---|
205 | switch(reader[ridx].typ) {
|
---|
206 | case R_DB2COM1:
|
---|
207 | case R_DB2COM2:
|
---|
208 | case R_MOUSE:
|
---|
209 | call (Phoenix_Reset(atr));
|
---|
210 | break;
|
---|
211 | #if defined(LIBUSB)
|
---|
212 | case R_SMART:
|
---|
213 | call (SR_Reset(&reader[ridx],atr));
|
---|
214 | break;
|
---|
215 | #endif
|
---|
216 | case R_INTERNAL:
|
---|
217 | #ifdef SCI_DEV
|
---|
218 | call (Sci_Activate());
|
---|
219 | call (Sci_Reset(atr));
|
---|
220 | #elif COOL
|
---|
221 | call (Cool_Reset(atr));
|
---|
222 | #endif
|
---|
223 | break;
|
---|
224 | default:
|
---|
225 | cs_log("ERROR ICC_Async_Activate: unknow reader type %i",reader[ridx].typ);
|
---|
226 | return ERROR;
|
---|
227 | }
|
---|
228 |
|
---|
229 | unsigned char atrarr[64];
|
---|
230 | unsigned int atr_size;
|
---|
231 | ATR_GetRaw(atr, atrarr, &atr_size);
|
---|
232 | cs_ri_log("ATR: %s", cs_hexdump(1, atrarr, atr_size));
|
---|
233 |
|
---|
234 | /* Get ICC convention */
|
---|
235 | if (ATR_GetConvention (atr, &(convention)) != ATR_OK) {
|
---|
236 | cs_log("ERROR: Could not read convention");
|
---|
237 | convention = 0;
|
---|
238 | protocol_type = 0;
|
---|
239 | return ERROR;
|
---|
240 | }
|
---|
241 |
|
---|
242 | protocol_type = ATR_PROTOCOL_TYPE_T0;
|
---|
243 |
|
---|
244 | unsigned short cs_ptyp_orig=cs_ptyp;
|
---|
245 | cs_ptyp=D_ATR;
|
---|
246 | int ret = Parse_ATR(atr, deprecated);
|
---|
247 | if (ret)
|
---|
248 | cs_log("ERROR: Parse_ATR returned error");
|
---|
249 | cs_ptyp=cs_ptyp_orig;
|
---|
250 | if (ret)
|
---|
251 | return ERROR;
|
---|
252 | cs_debug_mask (D_IFD, "IFD: Card in reader %s succesfully activated\n", reader[ridx].label);
|
---|
253 | return OK;
|
---|
254 | }
|
---|
255 |
|
---|
256 | int ICC_Async_CardWrite (unsigned char *cmd, unsigned short lc, unsigned char *rsp, unsigned short *lr)
|
---|
257 | {
|
---|
258 | APDU_Rsp *apdu_rsp = NULL;
|
---|
259 | int remain;
|
---|
260 | bool err = FALSE;
|
---|
261 |
|
---|
262 | call (Protocol_Command (cmd, lc, &apdu_rsp));
|
---|
263 | {
|
---|
264 | if (apdu_rsp != NULL) {
|
---|
265 | /* Copy APDU data to rsp */
|
---|
266 | remain = MAX ((short)APDU_Rsp_RawLen(apdu_rsp) - (*lr),0);
|
---|
267 | if (remain > 0) {
|
---|
268 | cs_log("MEMORY ERROR");
|
---|
269 | err = TRUE; //FIXME do I need this?
|
---|
270 | }
|
---|
271 | (*lr) = MIN ((*lr), (short)APDU_Rsp_RawLen (apdu_rsp));
|
---|
272 | memcpy (rsp, APDU_Rsp_Raw (apdu_rsp) + remain, (*lr));
|
---|
273 | APDU_Rsp_Delete (apdu_rsp);
|
---|
274 | }
|
---|
275 | else
|
---|
276 | (*lr) = 0;
|
---|
277 | }
|
---|
278 |
|
---|
279 | if (err) {
|
---|
280 | cs_log("ERROR creating APDU response");
|
---|
281 | return ERROR;
|
---|
282 | }
|
---|
283 |
|
---|
284 | return OK;
|
---|
285 | }
|
---|
286 |
|
---|
287 | int Protocol_Command (unsigned char * command, unsigned long command_len, APDU_Rsp ** rsp)
|
---|
288 | {
|
---|
289 | switch (protocol_type) {
|
---|
290 | case ATR_PROTOCOL_TYPE_T0:
|
---|
291 | call (Protocol_T0_Command (command, command_len, rsp));
|
---|
292 | break;
|
---|
293 | case ATR_PROTOCOL_TYPE_T1:
|
---|
294 | call (Protocol_T1_Command (command, command_len, rsp));
|
---|
295 | break;
|
---|
296 | case ATR_PROTOCOL_TYPE_T14:
|
---|
297 | call (Protocol_T14_ExchangeTPDU (command, command_len, rsp));
|
---|
298 | break;
|
---|
299 | default:
|
---|
300 | cs_log("Error, unknown protocol type %i",protocol_type);
|
---|
301 | return ERROR;
|
---|
302 | }
|
---|
303 | return OK;
|
---|
304 | }
|
---|
305 |
|
---|
306 | int ICC_Async_SetTimings (unsigned wait_etu)
|
---|
307 | {
|
---|
308 | read_timeout = ETU_to_ms(wait_etu);
|
---|
309 | cs_debug_mask(D_IFD, "Setting timeout to %i", wait_etu);
|
---|
310 | return OK;
|
---|
311 | }
|
---|
312 |
|
---|
313 | int ICC_Async_Transmit (unsigned size, BYTE * data)
|
---|
314 | {
|
---|
315 | cs_ddump_mask(D_IFD, data, size, "IFD Transmit: ");
|
---|
316 | BYTE *buffer = NULL, *sent;
|
---|
317 |
|
---|
318 | if (convention == ATR_CONVENTION_INVERSE && reader[ridx].typ <= R_MOUSE) {
|
---|
319 | buffer = (BYTE *) calloc(sizeof (BYTE), size);
|
---|
320 | memcpy (buffer, data, size);
|
---|
321 | ICC_Async_InvertBuffer (size, buffer);
|
---|
322 | sent = buffer;
|
---|
323 | }
|
---|
324 | else
|
---|
325 | sent = data;
|
---|
326 |
|
---|
327 | switch(reader[ridx].typ) {
|
---|
328 | case R_DB2COM1:
|
---|
329 | case R_DB2COM2:
|
---|
330 | case R_MOUSE:
|
---|
331 | call (Phoenix_Transmit (sent, size, icc_timings.block_delay, icc_timings.char_delay));
|
---|
332 | break;
|
---|
333 | #if defined(LIBUSB)
|
---|
334 | case R_SMART:
|
---|
335 | call (SR_Transmit(&reader[ridx], sent, size));
|
---|
336 | break;
|
---|
337 | #endif
|
---|
338 | case R_INTERNAL:
|
---|
339 | #ifdef COOL
|
---|
340 | call (Cool_Transmit(sent, size));
|
---|
341 | #elif SCI_DEV
|
---|
342 | call (Phoenix_Transmit (sent, size, 0, 0)); //the internal reader will provide the delay
|
---|
343 | #endif
|
---|
344 | break;
|
---|
345 | default:
|
---|
346 | cs_log("ERROR ICC_Async_Transmit: unknow reader type %i",reader[ridx].typ);
|
---|
347 | return ERROR;
|
---|
348 | }
|
---|
349 |
|
---|
350 | if (convention == ATR_CONVENTION_INVERSE && reader[ridx].typ <= R_MOUSE)
|
---|
351 | free (buffer);
|
---|
352 | cs_debug_mask(D_IFD, "IFD Transmit succesful");
|
---|
353 | return OK;
|
---|
354 | }
|
---|
355 |
|
---|
356 | int ICC_Async_Receive (unsigned size, BYTE * data)
|
---|
357 | {
|
---|
358 | switch(reader[ridx].typ) {
|
---|
359 | case R_DB2COM1:
|
---|
360 | case R_DB2COM2:
|
---|
361 | case R_MOUSE:
|
---|
362 | call (Phoenix_Receive (data, size, read_timeout));
|
---|
363 | break;
|
---|
364 | #if defined(LIBUSB)
|
---|
365 | case R_SMART:
|
---|
366 | call (SR_Receive(&reader[ridx], data, size));
|
---|
367 | break;
|
---|
368 | #endif
|
---|
369 | case R_INTERNAL:
|
---|
370 | #ifdef COOL
|
---|
371 | call (Cool_Receive(data, size));
|
---|
372 | #elif SCI_DEV
|
---|
373 | call (Phoenix_Receive (data, size, read_timeout));
|
---|
374 | #endif
|
---|
375 | break;
|
---|
376 | default:
|
---|
377 | cs_log("ERROR ICC_Async_Receive: unknow reader type %i",reader[ridx].typ);
|
---|
378 | return ERROR;
|
---|
379 | }
|
---|
380 |
|
---|
381 | if (convention == ATR_CONVENTION_INVERSE && reader[ridx].typ <= R_MOUSE)
|
---|
382 | ICC_Async_InvertBuffer (size, data);
|
---|
383 |
|
---|
384 | cs_ddump_mask(D_IFD, data, size, "IFD Received: ");
|
---|
385 | return OK;
|
---|
386 | }
|
---|
387 |
|
---|
388 | int ICC_Async_Close ()
|
---|
389 | { //FIXME this routine is never called!
|
---|
390 | cs_debug_mask (D_IFD, "IFD: Closing device %s", reader[ridx].device);
|
---|
391 |
|
---|
392 | switch(reader[ridx].typ) {
|
---|
393 | case R_DB2COM1:
|
---|
394 | case R_DB2COM2:
|
---|
395 | case R_MOUSE:
|
---|
396 | call (Phoenix_Close());
|
---|
397 | break;
|
---|
398 | #if defined(LIBUSB)
|
---|
399 | case R_SMART:
|
---|
400 | call (SR_Close(&reader[ridx]));
|
---|
401 | break;
|
---|
402 | #endif
|
---|
403 | case R_INTERNAL:
|
---|
404 | #ifdef SCI_DEV
|
---|
405 | /* Dectivate ICC */
|
---|
406 | call (Sci_Deactivate());
|
---|
407 | call (Phoenix_Close());
|
---|
408 | #endif
|
---|
409 | break;
|
---|
410 | default:
|
---|
411 | cs_log("ERROR ICC_Async_Close: unknow reader type %i",reader[ridx].typ);
|
---|
412 | return ERROR;
|
---|
413 | }
|
---|
414 |
|
---|
415 | cs_debug_mask (D_IFD, "IFD: Device %s succesfully closed", reader[ridx].device);
|
---|
416 | return OK;
|
---|
417 | }
|
---|
418 |
|
---|
419 | unsigned long ICC_Async_GetClockRate ()
|
---|
420 | {
|
---|
421 | switch (reader[ridx].cardmhz) {
|
---|
422 | case 357:
|
---|
423 | case 358:
|
---|
424 | return (372L * 9600L);
|
---|
425 | case 368:
|
---|
426 | return (384L * 9600L);
|
---|
427 | default:
|
---|
428 | return reader[ridx].cardmhz * 10000L;
|
---|
429 | }
|
---|
430 | }
|
---|
431 |
|
---|
432 | static void ICC_Async_InvertBuffer (unsigned size, BYTE * buffer)
|
---|
433 | {
|
---|
434 | uint i;
|
---|
435 |
|
---|
436 | for (i = 0; i < size; i++)
|
---|
437 | buffer[i] = ~(INVERT_BYTE (buffer[i]));
|
---|
438 | }
|
---|
439 |
|
---|
440 | static int Parse_ATR (ATR * atr, unsigned short deprecated)
|
---|
441 | {
|
---|
442 | BYTE FI = ATR_DEFAULT_FI;
|
---|
443 | //BYTE t = ATR_PROTOCOL_TYPE_T0;
|
---|
444 | double d = ATR_DEFAULT_D;
|
---|
445 | double n = ATR_DEFAULT_N;
|
---|
446 | int ret;
|
---|
447 |
|
---|
448 | int numprot = atr->pn;
|
---|
449 | //if there is a trailing TD, this number is one too high
|
---|
450 | BYTE tx;
|
---|
451 | if (ATR_GetInterfaceByte (atr, numprot-1, ATR_INTERFACE_BYTE_TD, &tx) == ATR_OK)
|
---|
452 | if ((tx & 0xF0) == 0)
|
---|
453 | numprot--;
|
---|
454 | int i,point;
|
---|
455 | char txt[50];
|
---|
456 | bool OffersT[3]; //T14 stored as T2
|
---|
457 | for (i = 0; i <= 2; i++)
|
---|
458 | OffersT[i] = FALSE;
|
---|
459 | for (i=1; i<= numprot; i++) {
|
---|
460 | point = 0;
|
---|
461 | if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TA, &tx) == ATR_OK) {
|
---|
462 | sprintf((char *)txt+point,"TA%i=%02X ",i,tx);
|
---|
463 | point +=7;
|
---|
464 | }
|
---|
465 | if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TB, &tx) == ATR_OK) {
|
---|
466 | sprintf((char *)txt+point,"TB%i=%02X ",i,tx);
|
---|
467 | point +=7;
|
---|
468 | }
|
---|
469 | if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TC, &tx) == ATR_OK) {
|
---|
470 | sprintf((char *)txt+point,"TC%i=%02X ",i,tx);
|
---|
471 | point +=7;
|
---|
472 | }
|
---|
473 | if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TD, &tx) == ATR_OK) {
|
---|
474 | sprintf((char *)txt+point,"TD%i=%02X ",i,tx);
|
---|
475 | point +=7;
|
---|
476 | tx &= 0X0F;
|
---|
477 | sprintf((char *)txt+point,"(T%i)",tx);
|
---|
478 | if (tx == 14)
|
---|
479 | OffersT[2] = TRUE;
|
---|
480 | else
|
---|
481 | OffersT[tx] = TRUE;
|
---|
482 | }
|
---|
483 | else {
|
---|
484 | sprintf((char *)txt+point,"no TD%i means T0",i);
|
---|
485 | OffersT[0] = TRUE;
|
---|
486 | }
|
---|
487 | cs_debug("%s",txt);
|
---|
488 | }
|
---|
489 |
|
---|
490 | int numprottype = 0;
|
---|
491 | for (i = 0; i <= 2; i++)
|
---|
492 | if (OffersT[i])
|
---|
493 | numprottype ++;
|
---|
494 | cs_debug("%i protocol types detected. Historical bytes: %s",numprottype, cs_hexdump(1,atr->hb,atr->hbn));
|
---|
495 |
|
---|
496 | ATR_GetParameter (atr, ATR_PARAMETER_N, &(n));
|
---|
497 | ATR_GetProtocolType(atr,1,&(protocol_type)); //get protocol from TD1
|
---|
498 | BYTE TA2;
|
---|
499 | bool SpecificMode = (ATR_GetInterfaceByte (atr, 2, ATR_INTERFACE_BYTE_TA, &TA2) == ATR_OK); //if TA2 present, specific mode, else negotiable mode
|
---|
500 | if (SpecificMode) {
|
---|
501 | protocol_type = TA2 & 0x0F;
|
---|
502 | if ((TA2 & 0x10) != 0x10) { //bit 5 set to 0 means F and D explicitly defined in interface characters
|
---|
503 | BYTE TA1;
|
---|
504 | if (ATR_GetInterfaceByte (atr, 1 , ATR_INTERFACE_BYTE_TA, &TA1) == ATR_OK) {
|
---|
505 | FI = TA1 >> 4;
|
---|
506 | ATR_GetParameter (atr, ATR_PARAMETER_D, &(d));
|
---|
507 | }
|
---|
508 | else {
|
---|
509 | FI = ATR_DEFAULT_FI;
|
---|
510 | d = ATR_DEFAULT_D;
|
---|
511 | }
|
---|
512 | }
|
---|
513 | else {
|
---|
514 | cs_log("Specific mode: speed 'implicitly defined', not sure how to proceed, assuming default values");
|
---|
515 | FI = ATR_DEFAULT_FI;
|
---|
516 | d = ATR_DEFAULT_D;
|
---|
517 | }
|
---|
518 | cs_debug("Specific mode: T%i, F=%.0f, D=%.6f, N=%.0f\n", protocol_type, (double) atr_f_table[FI], d, n);
|
---|
519 | }
|
---|
520 | else { //negotiable mode
|
---|
521 |
|
---|
522 | bool PPS_success = FALSE;
|
---|
523 | 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 old ISO 7816
|
---|
524 | if (NeedsPTS && deprecated == 0) {
|
---|
525 | // PTSS PTS0 PTS1 PCK
|
---|
526 | BYTE req[] = { 0xFF, 0x10, 0x00, 0x00 }; //we currently do not support PTS2, standard guardtimes
|
---|
527 | req[1]=0x10 | protocol_type; //PTS0 always flags PTS1 to be sent always
|
---|
528 | if (ATR_GetInterfaceByte (atr, 1, ATR_INTERFACE_BYTE_TA, &req[2]) != ATR_OK) //PTS1
|
---|
529 | req[2] = 0x11; //defaults FI and DI to 1
|
---|
530 | unsigned int len = sizeof(req);
|
---|
531 | ret = PPS_Exchange (req, &len);
|
---|
532 | if (ret == OK) {
|
---|
533 | FI = req[2] >> 4;
|
---|
534 | BYTE DI = req[2] & 0x0F;
|
---|
535 | d = (double) (atr_d_table[DI]);
|
---|
536 | PPS_success = TRUE;
|
---|
537 | cs_debug("PTS Succesfull, selected protocol: T%i, F=%.0f, D=%.6f, N=%.0f\n", protocol_type, (double) atr_f_table[FI], d, n);
|
---|
538 | }
|
---|
539 | else
|
---|
540 | cs_ddump(req,4,"PTS Failure, response:");
|
---|
541 | }
|
---|
542 |
|
---|
543 | //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)
|
---|
544 | if (!PPS_success) {//last PPS not succesfull
|
---|
545 | BYTE TA1;
|
---|
546 | if (ATR_GetInterfaceByte (atr, 1 , ATR_INTERFACE_BYTE_TA, &TA1) == ATR_OK) {
|
---|
547 | FI = TA1 >> 4;
|
---|
548 | ATR_GetParameter (atr, ATR_PARAMETER_D, &(d));
|
---|
549 | }
|
---|
550 | else { //do not obey TA1
|
---|
551 | FI = ATR_DEFAULT_FI;
|
---|
552 | d = ATR_DEFAULT_D;
|
---|
553 | }
|
---|
554 | if (NeedsPTS) {
|
---|
555 | if ((d == 32) || (d == 12) || (d == 20)) //those values were RFU in old table
|
---|
556 | d = 0; // viaccess cards that fail PTS need this
|
---|
557 | }
|
---|
558 |
|
---|
559 | 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);
|
---|
560 | }
|
---|
561 | }//end negotiable mode
|
---|
562 |
|
---|
563 | //make sure no zero values
|
---|
564 | double F = (double) atr_f_table[FI];
|
---|
565 | if (!F) {
|
---|
566 | FI = ATR_DEFAULT_FI;
|
---|
567 | cs_log("Warning: F=0 is invalid, forcing FI=%d", FI);
|
---|
568 | }
|
---|
569 | if (!d) {
|
---|
570 | d = ATR_DEFAULT_D;
|
---|
571 | cs_log("Warning: D=0 is invalid, forcing D=%.0f",d);
|
---|
572 | }
|
---|
573 |
|
---|
574 | if (deprecated == 0)
|
---|
575 | return InitCard (atr, FI, d, n, deprecated);
|
---|
576 | else
|
---|
577 | return InitCard (atr, ATR_DEFAULT_FI, ATR_DEFAULT_D, n, deprecated);
|
---|
578 | }
|
---|
579 |
|
---|
580 | static int PPS_Exchange (BYTE * params, unsigned *length)
|
---|
581 | {
|
---|
582 | BYTE confirm[PPS_MAX_LENGTH];
|
---|
583 | unsigned len_request, len_confirm;
|
---|
584 | int ret;
|
---|
585 |
|
---|
586 | len_request = PPS_GetLength (params);
|
---|
587 | params[len_request - 1] = PPS_GetPCK(params, len_request - 1);
|
---|
588 | cs_debug_mask (D_IFD,"PTS: Sending request: %s", cs_hexdump(1, params, len_request));
|
---|
589 |
|
---|
590 | /* Send PPS request */
|
---|
591 | call (ICC_Async_Transmit (len_request, params));
|
---|
592 |
|
---|
593 | /* Get PPS confirm */
|
---|
594 | call (ICC_Async_Receive (2, confirm));
|
---|
595 | len_confirm = PPS_GetLength (confirm);
|
---|
596 | call (ICC_Async_Receive (len_confirm - 2, confirm + 2));
|
---|
597 |
|
---|
598 | cs_debug_mask(D_IFD, "PTS: Receiving confirm: %s", cs_hexdump(1, confirm, len_confirm));
|
---|
599 | if ((len_request != len_confirm) || (memcmp (params, confirm, len_request)))
|
---|
600 | ret = ERROR;
|
---|
601 | else
|
---|
602 | ret = OK;
|
---|
603 |
|
---|
604 | /* Copy PPS handsake */
|
---|
605 | memcpy (params, confirm, len_confirm);
|
---|
606 | (*length) = len_confirm;
|
---|
607 | return ret;
|
---|
608 | }
|
---|
609 |
|
---|
610 | static unsigned PPS_GetLength (BYTE * block)
|
---|
611 | {
|
---|
612 | unsigned length = 3;
|
---|
613 |
|
---|
614 | if (PPS_HAS_PPS1 (block))
|
---|
615 | length++;
|
---|
616 |
|
---|
617 | if (PPS_HAS_PPS2 (block))
|
---|
618 | length++;
|
---|
619 |
|
---|
620 | if (PPS_HAS_PPS3 (block))
|
---|
621 | length++;
|
---|
622 |
|
---|
623 | return length;
|
---|
624 | }
|
---|
625 |
|
---|
626 | static unsigned int ETU_to_ms(unsigned long WWT)
|
---|
627 | {
|
---|
628 | #define CHAR_LEN 10L //character length in ETU, perhaps should be 9 when parity = none?
|
---|
629 | if (WWT > CHAR_LEN)
|
---|
630 | WWT -= CHAR_LEN;
|
---|
631 | else
|
---|
632 | WWT = 0;
|
---|
633 | double work_etu = 1000 / (double)current_baudrate;//FIXME sometimes work_etu should be used, sometimes initial etu
|
---|
634 | return (unsigned int) WWT * work_etu * reader[ridx].cardmhz / reader[ridx].mhz;
|
---|
635 | }
|
---|
636 |
|
---|
637 | static int ICC_Async_SetParity (unsigned short parity)
|
---|
638 | {
|
---|
639 | switch(reader[ridx].typ) {
|
---|
640 | case R_DB2COM1:
|
---|
641 | case R_DB2COM2:
|
---|
642 | case R_MOUSE:
|
---|
643 | call (IO_Serial_SetParity (parity));
|
---|
644 | break;
|
---|
645 | #if defined(LIBUSB)
|
---|
646 | case R_SMART:
|
---|
647 | reader[ridx].sr_config.inv= (convention == ATR_CONVENTION_INVERSE) ? 1: 0;
|
---|
648 | reader[ridx].sr_config.parity=parity;
|
---|
649 | call (SR_SetParity(&reader[ridx]));
|
---|
650 | break;
|
---|
651 | #endif
|
---|
652 | case R_INTERNAL:
|
---|
653 | return OK;
|
---|
654 | default:
|
---|
655 | cs_log("ERROR ICC_Async_SetParity: unknow reader type %i",reader[ridx].typ);
|
---|
656 | return ERROR;
|
---|
657 | }
|
---|
658 | return OK;
|
---|
659 | }
|
---|
660 |
|
---|
661 | static int SetRightParity (void)
|
---|
662 | {
|
---|
663 | //set right parity
|
---|
664 | unsigned short parity = PARITY_EVEN;
|
---|
665 | if (convention == ATR_CONVENTION_INVERSE)
|
---|
666 | parity = PARITY_ODD;
|
---|
667 | else if(protocol_type == ATR_PROTOCOL_TYPE_T14)
|
---|
668 | parity = PARITY_NONE;
|
---|
669 |
|
---|
670 | call (ICC_Async_SetParity(parity));
|
---|
671 |
|
---|
672 | #ifdef COOL
|
---|
673 | if (reader[ridx].typ != R_INTERNAL)
|
---|
674 | #endif
|
---|
675 | #if defined(LIBUSB)
|
---|
676 | if (reader[ridx].typ != R_SMART)
|
---|
677 | #endif
|
---|
678 | IO_Serial_Flush();
|
---|
679 | return OK;
|
---|
680 | }
|
---|
681 |
|
---|
682 | static int InitCard (ATR * atr, BYTE FI, double d, double n, unsigned short deprecated)
|
---|
683 | {
|
---|
684 | double P,I;
|
---|
685 | double F;
|
---|
686 | unsigned long BGT, edc, EGT, CGT, WWT = 0;
|
---|
687 | unsigned int GT;
|
---|
688 | unsigned long gt_ms;
|
---|
689 | current_baudrate = DEFAULT_BAUDRATE;
|
---|
690 |
|
---|
691 | //set the amps and the volts according to ATR
|
---|
692 | if (ATR_GetParameter(atr, ATR_PARAMETER_P, &P) != ATR_OK)
|
---|
693 | P = 0;
|
---|
694 | if (ATR_GetParameter(atr, ATR_PARAMETER_I, &I) != ATR_OK)
|
---|
695 | I = 0;
|
---|
696 |
|
---|
697 | //set clock speed to max if internal reader
|
---|
698 | if(reader[ridx].typ > R_MOUSE)
|
---|
699 | if (reader[ridx].mhz == 357 || reader[ridx].mhz == 358) //no overclocking
|
---|
700 | reader[ridx].mhz = atr_fs_table[FI] / 10000; //we are going to clock the card to this nominal frequency
|
---|
701 |
|
---|
702 | //set clock speed/baudrate must be done before timings
|
---|
703 | //because current_baudrate is used in calculation of timings
|
---|
704 | F = (double) atr_f_table[FI];
|
---|
705 |
|
---|
706 | if (deprecated == 0)
|
---|
707 | if (protocol_type != ATR_PROTOCOL_TYPE_T14) { //dont switch for T14
|
---|
708 | unsigned long baud_temp = d * ICC_Async_GetClockRate () / F;
|
---|
709 | if (reader[ridx].typ <= R_MOUSE)
|
---|
710 | call (Phoenix_SetBaudrate (baud_temp));
|
---|
711 | cs_debug_mask(D_IFD, "Setting baudrate to %lu", baud_temp);
|
---|
712 | current_baudrate = baud_temp; //this is needed for all readers to calculate work_etu for timings
|
---|
713 | }
|
---|
714 |
|
---|
715 | //set timings according to ATR
|
---|
716 | read_timeout = 0;
|
---|
717 | icc_timings.block_delay = 0;
|
---|
718 | icc_timings.char_delay = 0;
|
---|
719 |
|
---|
720 | if (n == 255) //Extra Guard Time
|
---|
721 | EGT = 0;
|
---|
722 | else
|
---|
723 | EGT = n;
|
---|
724 | GT = EGT + 12; //Guard Time in ETU
|
---|
725 | gt_ms = ETU_to_ms(GT);
|
---|
726 |
|
---|
727 | switch (protocol_type) {
|
---|
728 | case ATR_PROTOCOL_TYPE_T0:
|
---|
729 | case ATR_PROTOCOL_TYPE_T14:
|
---|
730 | {
|
---|
731 | BYTE wi;
|
---|
732 | /* Integer value WI = TC2, by default 10 */
|
---|
733 | #ifndef PROTOCOL_T0_USE_DEFAULT_TIMINGS
|
---|
734 | if (ATR_GetInterfaceByte (atr, 2, ATR_INTERFACE_BYTE_TC, &(wi)) != ATR_OK)
|
---|
735 | #endif
|
---|
736 | wi = DEFAULT_WI;
|
---|
737 |
|
---|
738 | // WWT = 960 * WI * (Fi / f) * 1000 milliseconds
|
---|
739 | WWT = (unsigned long) 960 * wi; //in ETU
|
---|
740 | if (protocol_type == ATR_PROTOCOL_TYPE_T14)
|
---|
741 | WWT >>= 1; //is this correct?
|
---|
742 |
|
---|
743 | read_timeout = ETU_to_ms(WWT);
|
---|
744 | icc_timings.block_delay = gt_ms;
|
---|
745 | icc_timings.char_delay = gt_ms;
|
---|
746 | 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);
|
---|
747 | cs_debug_mask (D_IFD,"Protocol: T=%i: WWT=%d, Clockrate=%lu\n", protocol_type, (int)(WWT), ICC_Async_GetClockRate());
|
---|
748 | }
|
---|
749 | break;
|
---|
750 | case ATR_PROTOCOL_TYPE_T1:
|
---|
751 | {
|
---|
752 | BYTE ta, tb, tc, cwi, bwi;
|
---|
753 |
|
---|
754 | // Set IFSC
|
---|
755 | if (ATR_GetInterfaceByte (atr, 3, ATR_INTERFACE_BYTE_TA, &ta) == ATR_NOT_FOUND)
|
---|
756 | ifsc = DEFAULT_IFSC;
|
---|
757 | else if ((ta != 0x00) && (ta != 0xFF))
|
---|
758 | ifsc = ta;
|
---|
759 | else
|
---|
760 | ifsc = DEFAULT_IFSC;
|
---|
761 |
|
---|
762 | // Towitoko does not allow IFSC > 251 //FIXME not sure whether this limitation still exists
|
---|
763 | //ifsc = MIN (ifsc, MAX_IFSC);
|
---|
764 |
|
---|
765 | //FIXME workaround for Smargo until native mode works
|
---|
766 | if(reader[ridx].smargopatch == 1)
|
---|
767 | ifsc = MIN (ifsc, 28);
|
---|
768 | else
|
---|
769 | ifsc = MIN (ifsc, MAX_IFSC);
|
---|
770 |
|
---|
771 | #ifndef PROTOCOL_T1_USE_DEFAULT_TIMINGS
|
---|
772 | // Calculate CWI and BWI
|
---|
773 | if (ATR_GetInterfaceByte (atr, 3, ATR_INTERFACE_BYTE_TB, &tb) == ATR_NOT_FOUND)
|
---|
774 | {
|
---|
775 | #endif
|
---|
776 | cwi = DEFAULT_CWI;
|
---|
777 | bwi = DEFAULT_BWI;
|
---|
778 | #ifndef PROTOCOL_T1_USE_DEFAULT_TIMINGS
|
---|
779 | }
|
---|
780 | else
|
---|
781 | {
|
---|
782 | cwi = tb & 0x0F;
|
---|
783 | bwi = tb >> 4;
|
---|
784 | }
|
---|
785 | #endif
|
---|
786 |
|
---|
787 | // Set CWT = (2^CWI + 11) work etu
|
---|
788 | CWT = (unsigned short) (((1<<cwi) + 11)); // in ETU
|
---|
789 |
|
---|
790 | // Set BWT = (2^BWI * 960 + 11) work etu
|
---|
791 | BWT = (unsigned short)((1<<bwi) * 960 * 372 * 9600 / ICC_Async_GetClockRate() ) + 11 ;
|
---|
792 |
|
---|
793 | // Set BGT = 22 * work etu
|
---|
794 | BGT = 22L; //in ETU
|
---|
795 |
|
---|
796 | if (n == 255)
|
---|
797 | CGT = 11L; //in ETU
|
---|
798 | else
|
---|
799 | CGT = GT;
|
---|
800 |
|
---|
801 | // Set the error detection code type
|
---|
802 | if (ATR_GetInterfaceByte (atr, 3, ATR_INTERFACE_BYTE_TC, &tc) == ATR_NOT_FOUND)
|
---|
803 | edc = EDC_LRC;
|
---|
804 | else
|
---|
805 | edc = tc & 0x01;
|
---|
806 |
|
---|
807 | // Set initial send sequence (NS)
|
---|
808 | ns = 1;
|
---|
809 |
|
---|
810 | 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");
|
---|
811 |
|
---|
812 | read_timeout = ETU_to_ms(BWT);
|
---|
813 | icc_timings.block_delay = ETU_to_ms(BGT);
|
---|
814 | icc_timings.char_delay = ETU_to_ms(CGT);
|
---|
815 | 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);
|
---|
816 | }
|
---|
817 | break;
|
---|
818 | default:
|
---|
819 | return ERROR;
|
---|
820 | break;
|
---|
821 | }//switch
|
---|
822 |
|
---|
823 | call (SetRightParity ());
|
---|
824 |
|
---|
825 | //write settings to internal device
|
---|
826 | if(reader[ridx].typ == R_INTERNAL) {
|
---|
827 | #ifdef SCI_DEV
|
---|
828 | double F = (double) atr_f_table[FI];
|
---|
829 | unsigned long ETU = 0;
|
---|
830 | //for Irdeto T14 cards, do not set ETU
|
---|
831 | if (!(atr->hbn >= 6 && !memcmp(atr->hb, "IRDETO", 6) && protocol_type == ATR_PROTOCOL_TYPE_T14))
|
---|
832 | ETU = F / d;
|
---|
833 | call (Sci_WriteSettings (protocol_type, reader[ridx].mhz / 100, ETU, WWT, BWT, CWT, EGT, (unsigned char)P, (unsigned char)I));
|
---|
834 | #elif COOL
|
---|
835 | call (Cool_SetClockrate(reader[ridx].mhz));
|
---|
836 | call (Cool_WriteSettings (BWT, CWT, EGT, BGT));
|
---|
837 | #endif //COOL
|
---|
838 | }
|
---|
839 | #if defined(LIBUSB)
|
---|
840 | if (reader[ridx].typ == R_SMART)
|
---|
841 | SR_WriteSettings(&reader[ridx], (unsigned short) atr_f_table[FI], (BYTE)d, (BYTE)EGT, (BYTE)protocol_type);
|
---|
842 | #endif
|
---|
843 | 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);
|
---|
844 |
|
---|
845 | //IFS setting in case of T1
|
---|
846 | if ((protocol_type == ATR_PROTOCOL_TYPE_T1) && (ifsc != DEFAULT_IFSC)) {
|
---|
847 | APDU_Rsp ** rsp;
|
---|
848 | unsigned char tmp[] = { 0x21, 0xC1, 0x01, 0x00, 0x00 };
|
---|
849 | tmp[3] = ifsc; // Information Field size
|
---|
850 | tmp[4] = ifsc ^ 0xE1;
|
---|
851 | Protocol_T1_Command (tmp, sizeof(tmp), rsp);
|
---|
852 | }
|
---|
853 | return OK;
|
---|
854 | }
|
---|
855 |
|
---|
856 | static BYTE PPS_GetPCK (BYTE * block, unsigned length)
|
---|
857 | {
|
---|
858 | BYTE pck;
|
---|
859 | unsigned i;
|
---|
860 |
|
---|
861 | pck = block[0];
|
---|
862 | for (i = 1; i < length; i++)
|
---|
863 | pck ^= block[i];
|
---|
864 |
|
---|
865 | return pck;
|
---|
866 | }
|
---|