source: trunk/reader-common.c@ 3181

Last change on this file since 3181 was 3181, checked in by dingo35, 10 years ago

Adding threadsafety FIXMEs, feel free to join checking..

File size: 11.9 KB
Line 
1//FIXME Not checked on threadsafety yet; after checking please remove this line
2#include "globals.h"
3#include "reader-common.h"
4#include "defines.h"
5#include "atr.h"
6#include "icc_async_exports.h"
7#include "csctapi/ifd_sc8in1.h"
8#ifdef HAVE_PCSC
9#include "csctapi/ifd_pcsc.h"
10#endif
11#ifdef AZBOX
12#include "csctapi/ifd_azbox.h"
13#endif
14
15#if defined(TUXBOX) && defined(PPC) //dbox2 only
16#include "csctapi/mc_global.h"
17static int reader_device_type(struct s_reader * reader)
18{
19 int rc=reader->typ;
20 struct stat sb;
21 if (reader->typ == R_MOUSE)
22 {
23 if (!stat(reader->device, &sb))
24 {
25 if (S_ISCHR(sb.st_mode))
26 {
27 int dev_major, dev_minor;
28 dev_major=major(sb.st_rdev);
29 dev_minor=minor(sb.st_rdev);
30 if (((dev_major==4) || (dev_major==5)))
31 switch(dev_minor & 0x3F)
32 {
33 case 0: rc=R_DB2COM1; break;
34 case 1: rc=R_DB2COM2; break;
35 }
36 cs_debug("device is major: %d, minor: %d, typ=%d", dev_major, dev_minor, rc);
37 }
38 }
39 }
40 reader->typ = rc;
41 return(rc);
42}
43#endif
44
45static void reader_nullcard(struct s_reader * reader)
46{
47 reader->card_system=0;
48 memset(reader->hexserial, 0 , sizeof(reader->hexserial));
49 memset(reader->prid , 0xFF, sizeof(reader->prid ));
50 memset(reader->caid , 0 , sizeof(reader->caid ));
51 memset(reader->availkeys, 0 , sizeof(reader->availkeys));
52 reader->acs=0;
53 reader->nprov=0;
54}
55
56int reader_cmd2icc(struct s_reader * reader, const uchar *buf, const int l, uchar * cta_res, ushort * p_cta_lr)
57{
58 int rc;
59#ifdef HAVE_PCSC
60 if (reader->typ == R_PCSC) {
61 return (pcsc_reader_do_api(reader, buf, cta_res, p_cta_lr,l));
62 }
63
64#endif
65
66 *p_cta_lr=CTA_RES_LEN-1; //FIXME not sure whether this one is necessary
67 int cs_ptyp_orig=client[cs_idx].cs_ptyp;
68 client[cs_idx].cs_ptyp=D_DEVICE;
69 if (reader->typ == R_SC8in1) {
70 pthread_mutex_lock(&sc8in1);
71 cs_debug("SC8in1: locked for CardWrite of slot %i", reader->slot);
72 Sc8in1_Selectslot(reader, reader->slot);
73 }
74 cs_ddump(buf, l, "write to cardreader %s:",reader->label);
75 rc=ICC_Async_CardWrite(reader, buf, (unsigned short)l, cta_res, p_cta_lr);
76 cs_ddump(cta_res, *p_cta_lr, "answer from cardreader %s:", reader->label);
77 if (reader->typ == R_SC8in1) {
78 cs_debug("SC8in1: unlocked for CardWrite of slot %i", reader->slot);
79 pthread_mutex_unlock(&sc8in1);
80 }
81 client[cs_idx].cs_ptyp=cs_ptyp_orig;
82 return rc;
83}
84
85#define CMD_LEN 5
86
87int card_write(struct s_reader * reader, const uchar *cmd, const uchar *data, uchar *response, ushort * response_length)
88{
89 uchar buf[260];
90 // always copy to be able to be able to use const buffer without changing all code
91 memcpy(buf, cmd, CMD_LEN);
92
93 if (data) {
94 if (cmd[4]) memcpy(buf+CMD_LEN, data, cmd[4]);
95 return(reader_cmd2icc(reader, buf, CMD_LEN+cmd[4], response, response_length));
96 }
97 else
98 return(reader_cmd2icc(reader, buf, CMD_LEN, response, response_length));
99}
100
101int check_sct_len(const uchar *data, int off)
102{
103 int l = SCT_LEN(data);
104 if (l+off > MAX_LEN) {
105 cs_debug("check_sct_len(): smartcard section too long %d > %d", l, MAX_LEN-off);
106 l = -1;
107 }
108 return(l);
109}
110
111
112static int reader_card_inserted(struct s_reader * reader)
113{
114#ifndef USE_GPIO
115 if ((reader->detect&0x7f) > 3)
116 return 1;
117#endif
118#ifdef HAVE_PCSC
119 if (reader->typ == R_PCSC) {
120 return(pcsc_check_card_inserted(reader));
121 }
122#endif
123 int card;
124 int cs_ptyp_orig=client[cs_idx].cs_ptyp;
125 client[cs_idx].cs_ptyp=D_IFD;
126 if (ICC_Async_GetStatus (reader, &card)) {
127 cs_log("Error getting status of terminal.");
128 return 0; //corresponds with no card inside!!
129 }
130 client[cs_idx].cs_ptyp=cs_ptyp_orig;
131 return (card);
132}
133
134static int reader_activate_card(struct s_reader * reader, ATR * atr, unsigned short deprecated)
135{
136 int i;
137#ifdef HAVE_PCSC
138 unsigned char atrarr[64];
139 ushort atr_size = 0;
140 if (reader->typ == R_PCSC) {
141 if (pcsc_activate_card(reader, atrarr, &atr_size))
142 return (ATR_InitFromArray (atr, atrarr, atr_size) == ATR_OK);
143 else
144 return 0;
145 }
146#endif
147 if (!reader_card_inserted(reader))
148 return 0;
149
150 /* Activate card */
151 int cs_ptyp_orig=client[cs_idx].cs_ptyp;
152 client[cs_idx].cs_ptyp=D_DEVICE;
153 if (reader->typ == R_SC8in1) {
154 pthread_mutex_lock(&sc8in1);
155 cs_debug_mask(D_ATR, "SC8in1: locked for Activation of slot %i", reader->slot);
156 Sc8in1_Selectslot(reader, reader->slot);
157 }
158 for (i=0; i<5; i++) {
159 if (!ICC_Async_Activate(reader, atr, deprecated)) {
160 i = 100;
161 break;
162 }
163 cs_log("Error activating card.");
164 cs_sleepms(500);
165 }
166 if (reader->typ == R_SC8in1) {
167 cs_debug_mask(D_ATR, "SC8in1: unlocked for Activation of slot %i", reader->slot);
168 pthread_mutex_unlock(&sc8in1);
169 }
170 client[cs_idx].cs_ptyp=cs_ptyp_orig;
171 if (i<100) return(0);
172
173 reader->init_history_pos=0;
174#ifdef CS_RDR_INIT_HIST
175 memset(reader->init_history, 0, sizeof(reader->init_history));
176#endif
177// cs_ri_log("ATR: %s", cs_hexdump(1, atr, atr_size));//FIXME
178 cs_sleepms(1000);
179 return(1);
180}
181
182void do_emm_from_file(struct s_reader * reader)
183{
184 //now here check whether we have EMM's on file to load and write to card:
185 if (reader->emmfile != NULL) {//readnano has something filled in
186
187 //handling emmfile
188 char token[256];
189 FILE *fp;
190 size_t result;
191 if ((reader->emmfile[0] == '/'))
192 sprintf (token, "%s", reader->emmfile); //pathname included
193 else
194 sprintf (token, "%s%s", cs_confdir, reader->emmfile); //only file specified, look in confdir for this file
195
196 if (!(fp = fopen (token, "rb")))
197 cs_log ("ERROR: Cannot open EMM file '%s' (errno=%d)\n", token, errno);
198 else {
199 EMM_PACKET *eptmp;
200 eptmp = malloc (sizeof(EMM_PACKET));
201 result = fread (eptmp, sizeof(EMM_PACKET), 1, fp);
202 fclose (fp);
203
204 uchar old_b_nano = reader->b_nano[eptmp->emm[0]]; //save old b_nano value
205 reader->b_nano[eptmp->emm[0]] &= 0xfc; //clear lsb and lsb+1, so no blocking, and no saving for this nano
206
207 //if (!reader_do_emm (eptmp))
208 if (!reader_emm (reader, eptmp))
209 cs_log ("ERROR: EMM read from file %s NOT processed correctly!", token);
210
211 reader->b_nano[eptmp->emm[0]] = old_b_nano; //restore old block/save settings
212 free (reader->emmfile);
213 reader->emmfile = NULL; //clear emmfile, so no reading anymore
214
215 free(eptmp);
216 eptmp = NULL;
217 }
218 }
219}
220
221void reader_card_info(struct s_reader * reader)
222{
223 if ((reader->card_status == CARD_NEED_INIT) || (reader->card_status == CARD_INSERTED))
224 {
225 client[cs_idx].last=time((time_t)0);
226 cs_ri_brk(reader, 0);
227 do_emm_from_file(reader);
228
229 if (cardsystem[reader->card_system-1].card_info) {
230 cardsystem[reader->card_system-1].card_info(reader);
231 }
232 }
233}
234
235static int reader_get_cardsystem(struct s_reader * reader, ATR atr)
236{
237 int i;
238 for (i=0; i<CS_MAX_MOD; i++) {
239 if (cardsystem[i].card_init) {
240 if (cardsystem[i].card_init(reader, atr)) {
241 reader->card_system=i+1;
242 cs_log("found cardsystem");
243 break;
244 }
245 }
246 }
247
248 if (reader->card_system==0)
249 cs_ri_log(reader, "card system not supported");
250
251 cs_ri_brk(reader, 1);
252
253 return(reader->card_system);
254}
255
256static int reader_reset(struct s_reader * reader)
257{
258 reader_nullcard(reader);
259 ATR atr;
260 unsigned short int ret = 0;
261#ifdef AZBOX
262 int i;
263 if (reader->typ == R_INTERNAL) {
264 if (reader->mode != -1) {
265 Azbox_SetMode(reader->mode);
266 if (!reader_activate_card(reader, &atr, 0)) return(0);
267 ret = reader_get_cardsystem(reader, atr);
268 } else {
269 for (i = 0; i < AZBOX_MODES; i++) {
270 Azbox_SetMode(i);
271 if (!reader_activate_card(reader, &atr, 0)) return(0);
272 ret = reader_get_cardsystem(reader, atr);
273 if (ret)
274 break;
275 }
276 }
277 } else {
278#endif
279 unsigned short int deprecated;
280 for (deprecated = reader->deprecated; deprecated < 2; deprecated++) {
281 if (!reader_activate_card(reader, &atr, deprecated)) return(0);
282 ret = reader_get_cardsystem(reader, atr);
283 if (ret)
284 break;
285 if (!deprecated)
286 cs_log("Normal mode failed, reverting to Deprecated Mode");
287 }
288#ifdef AZBOX
289 }
290#endif
291 return(ret);
292}
293
294int reader_device_init(struct s_reader * reader)
295{
296#ifdef HAVE_PCSC
297 if (reader->typ == R_PCSC) {
298 return (pcsc_reader_init(reader, reader->device));
299 }
300#endif
301
302 int rc = -1; //FIXME
303 int cs_ptyp_orig=client[cs_idx].cs_ptyp;
304 client[cs_idx].cs_ptyp=D_DEVICE;
305#if defined(TUXBOX) && defined(PPC)
306 struct stat st;
307 if (!stat(DEV_MULTICAM, &st))
308 reader->typ = reader_device_type(reader);
309#endif
310 if (ICC_Async_Device_Init(reader))
311 cs_log("Cannot open device: %s", reader->device);
312 else
313 rc = OK;
314 cs_debug("ct_init on %s: %d", reader->device, rc);
315 client[cs_idx].cs_ptyp=cs_ptyp_orig;
316 return((rc!=OK) ? 2 : 0);
317}
318
319int reader_checkhealth(struct s_reader * reader)
320{
321 if (reader_card_inserted(reader))
322 {
323 if (reader->card_status == NO_CARD)
324 {
325 cs_log("card detected");
326 reader->card_status = CARD_NEED_INIT;
327 if (!reader_reset(reader))
328 {
329 reader->card_status = CARD_FAILURE;
330 cs_log("card initializing error");
331 }
332 else
333 {
334 client[cs_idx].au = client[cs_idx].ridx;
335 reader_card_info(reader);
336 reader->card_status = CARD_INSERTED;
337 }
338
339 int i;
340 for( i=1; i<CS_MAXPID; i++ ) {
341 if( client[i].pid && client[i].typ=='c' && client[i].usr[0] && ph[client[i].ctyp].type & MOD_CONN_NET) {
342 //kill(client[i].pid, SIGQUIT);
343 }
344 }
345 }
346 }
347 else
348 {
349 if (reader->card_status == CARD_INSERTED)
350 {
351 reader_nullcard(reader);
352 client[cs_idx].lastemm = 0;
353 client[cs_idx].lastecm = 0;
354 client[cs_idx].au = -1;
355 extern int io_serial_need_dummy_char;
356 io_serial_need_dummy_char = 0;
357 cs_log("card ejected slot = %i", reader->slot);
358 }
359 reader->card_status = NO_CARD;
360 }
361 return reader->card_status == CARD_INSERTED;
362}
363
364void reader_post_process(struct s_reader * reader)
365{
366 // some systems eg. nagra2/3 needs post process after receiving cw from card
367 // To save ECM/CW time we added this function after writing ecm answer
368
369 if (cardsystem[reader->card_system-1].post_process) {
370 cardsystem[reader->card_system-1].post_process(reader);
371 }
372}
373
374int reader_ecm(struct s_reader * reader, ECM_REQUEST *er)
375{
376 int rc=-1;
377 if( (rc=reader_checkhealth(reader)) )
378 {
379 if((reader->caid[0] >> 8) == ((er->caid >> 8) & 0xFF))
380 {
381 client[cs_idx].last_srvid=er->srvid;
382 client[cs_idx].last_caid=er->caid;
383 client[cs_idx].last=time((time_t)0);
384
385 if (cardsystem[reader->card_system-1].do_ecm)
386 rc=cardsystem[reader->card_system-1].do_ecm(reader, er);
387 else
388 rc=0;
389
390 }
391 else
392 rc=0;
393 }
394 return(rc);
395}
396
397int reader_get_emm_type(EMM_PACKET *ep, struct s_reader * rdr) //rdr differs from calling reader!
398{
399 cs_debug_mask(D_EMM,"Entered reader_get_emm_type cardsystem %i",rdr->card_system);
400 int rc;
401
402 if (rdr->card_system<1)
403 return 0;
404
405 if (cardsystem[rdr->card_system-1].get_emm_type)
406 rc=cardsystem[rdr->card_system-1].get_emm_type(ep, rdr);
407 else
408 rc=0;
409
410 return rc;
411}
412
413int get_cardsystem(ushort caid) {
414 int i,j;
415 for (i=0; i<CS_MAX_MOD; i++) {
416 if (cardsystem[i].caids) {
417 for (j=0;j<2;j++) {
418 if ((cardsystem[i].caids[j]==caid >> 8)) {
419 return i+1;
420 }
421 }
422 }
423 }
424 return 0;
425}
426
427void get_emm_filter(struct s_reader * rdr, uchar *filter) {
428 filter[0]=0xFF;
429 filter[1]=0;
430
431 if (cardsystem[rdr->card_system-1].get_emm_filter) {
432 cardsystem[rdr->card_system-1].get_emm_filter(rdr, filter);
433 }
434
435 return;
436}
437
438int reader_emm(struct s_reader * reader, EMM_PACKET *ep)
439{
440 int rc=-1;
441
442 rc=reader_checkhealth(reader);
443 if (rc)
444 {
445 if (reader->b_nano[ep->emm[0]] & 0x01) //should this nano be blcoked?
446 return 3;
447
448 if (cardsystem[reader->card_system-1].do_emm)
449 rc=cardsystem[reader->card_system-1].do_emm(reader, ep);
450 else
451 rc=0;
452 }
453 return(rc);
454}
455
456int check_emm_cardsystem(struct s_reader * rdr, EMM_PACKET *ep)
457{
458 return (rdr->fd && (rdr->caid[0] == b2i(2,ep->caid) || rdr->typ == R_CCCAM));
459}
460
461void reader_device_close(struct s_reader * reader)
462{
463#ifdef HAVE_PCSC
464 if (reader->typ == R_PCSC)
465 pcsc_close(reader);
466 else
467#endif
468 ICC_Async_Close(reader);
469}
Note: See TracBrowser for help on using the repository browser.