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