source: trunk/reader-common.c@ 3168

Last change on this file since 3168 was 3168, checked in by _network, 10 years ago

merge modular branch with trunk

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