5 #include "module-gbox.h"
6 #include "module-led.h"
8 #include "oscam-client.h"
10 #include "oscam-emm.h"
11 #include "oscam-net.h"
12 #include "oscam-time.h"
13 #include "oscam-work.h"
14 #include "oscam-reader.h"
15 #include "reader-common.h"
16 //#include "csctapi/atr.h"
17 #include "csctapi/icc_async.h"
18 #include "readers.h" // required by the EMU reader
20 extern const struct s_cardsystem
*cardsystems
[];
21 extern char *RDR_CD_TXT
[];
23 int32_t check_sct_len(const uint8_t *data
, int32_t off
)
25 int32_t len
= SCT_LEN(data
);
26 if(len
+ off
> MAX_LEN
)
28 cs_log_dbg(D_TRACE
| D_READER
, "check_sct_len(): smartcard section too long %d > %d", len
, MAX_LEN
- off
);
34 static void reader_nullcard(struct s_reader
*reader
)
36 reader
->csystem_active
= false;
37 reader
->csystem
= NULL
;
38 memset(reader
->hexserial
, 0, sizeof(reader
->hexserial
));
39 memset(reader
->prid
, 0xFF, sizeof(reader
->prid
));
40 memset(reader
->sa
, 0, sizeof(reader
->sa
));
41 memset(reader
->emm84
, 0, sizeof(reader
->emm84
));
42 memset(reader
->emm83s
, 0, sizeof(reader
->emm83s
));
43 memset(reader
->emm83u
, 0, sizeof(reader
->emm83u
));
44 memset(reader
->emm87
, 0, sizeof(reader
->emm87
));
47 cs_clear_entitlement(reader
);
50 int32_t reader_cmd2icc(struct s_reader
*reader
, const uint8_t *buf
, const int32_t l
, uint8_t *cta_res
, uint16_t *p_cta_lr
)
53 *p_cta_lr
= CTA_RES_LEN
- 1; // FIXME not sure whether this one is necessary
54 rdr_log_dump_dbg(reader
, D_READER
, buf
, l
, "write to cardreader");
55 rc
= ICC_Async_CardWrite(reader
, (uint8_t *)buf
, (uint16_t)l
, cta_res
, p_cta_lr
);
61 int32_t card_write(struct s_reader
*reader
, const uint8_t *cmd
, const uint8_t *data
, uint8_t *response
, uint16_t *response_length
)
63 int32_t datalen
= MAX_ECM_SIZE
; // default datalen is max ecm size defined
64 uint8_t buf
[MAX_ECM_SIZE
+ CMD_LEN
];
65 // always copy to be able to be able to use const buffer without changing all code
66 memcpy(buf
, cmd
, CMD_LEN
); // copy command
74 memcpy(buf
+ CMD_LEN
, data
, datalen
);
75 return (reader_cmd2icc(reader
, buf
, CMD_LEN
+ datalen
, response
, response_length
));
78 { return (reader_cmd2icc(reader
, buf
, CMD_LEN
, response
, response_length
)); }
81 static inline int reader_use_gpio(struct s_reader
*reader
)
83 return reader
->use_gpio
&& reader
->detect
> 4;
86 static int32_t reader_card_inserted(struct s_reader
*reader
)
88 if(!reader_use_gpio(reader
) && (reader
->detect
& 0x7f) > 3)
92 if(ICC_Async_GetStatus(reader
, &card
))
94 rdr_log(reader
, "Error getting card status.");
95 return 0; // corresponds with no card inside!!
100 static int32_t reader_activate_card(struct s_reader
*reader
, ATR
*atr
, uint16_t deprecated
)
104 if(reader
->card_status
!= CARD_NEED_INIT
)
108 for(i
= 0; i
< 3; i
++)
110 ret
= ICC_Async_Activate(reader
, atr
, deprecated
);
113 rdr_log(reader
, "Error activating card.");
114 led_status_card_activation_error();
117 if(ret
) { return (0); }
119 //rdr_log("ATR: %s", cs_hexdump(1, atr, atr_size, tmp, sizeof(tmp))); // FIXME
124 void cardreader_get_card_info(struct s_reader
*reader
)
126 if((reader
->card_status
== CARD_NEED_INIT
) || (reader
->card_status
== CARD_INSERTED
))
128 struct s_client
*cl
= reader
->client
;
130 { cl
->last
= time((time_t *)0); }
132 if(reader
->csystem_active
&& reader
->csystem
&& reader
->csystem
->card_info
)
134 reader
->csystem
->card_info(reader
);
139 void cardreader_poll_status(struct s_reader
*reader
)
141 if (reader
&& reader
->card_status
== CARD_INSERTED
)
143 if (reader
->csystem_active
&& reader
->csystem
&& reader
->csystem
->poll_status
)
144 { reader
->csystem
->poll_status(reader
); }
148 static int32_t reader_get_cardsystem(struct s_reader
*reader
, ATR
*atr
)
153 if(reader
->typ
== R_EMU
)
155 NULLFREE(reader
->csystem_data
);
156 rdr_log(reader
, "found card system %s", reader_emu
.desc
);
157 reader
->csystem
= &reader_emu
;
158 reader
->csystem_active
= true;
159 led_status_found_cardsystem();
160 return (reader
->csystem_active
);
164 for(i
= 0; cardsystems
[i
]; i
++)
166 NULLFREE(reader
->csystem_data
);
167 const struct s_cardsystem
*csystem
= cardsystems
[i
];
168 if(csystem
->card_init(reader
, atr
))
170 rdr_log(reader
, "found card system %s", csystem
->desc
);
171 reader
->csystem
= csystem
;
172 reader
->csystem_active
= true;
173 led_status_found_cardsystem();
178 // On error free allocated card system data if any
179 if(csystem
->card_done
)
180 csystem
->card_done(reader
);
181 NULLFREE(reader
->csystem_data
);
185 if(!reader
->csystem_active
)
187 rdr_log(reader
, "card system not supported");
188 led_status_unsupported_card_system();
191 return (reader
->csystem_active
);
194 void cardreader_do_reset(struct s_reader
*reader
)
196 reader_nullcard(reader
);
202 if (reader
->typ
== R_SMART
&& reader
->smartdev_found
>= 4) j
= 1; else j
= 1; // back to a single start
204 for (i
= 0; i
< j
; i
++)
206 ret
= ICC_Async_Reset(reader
, &atr
, reader_activate_card
, reader_get_cardsystem
);
216 if (reader
->typ
== R_SMART
&& reader
->smartdev_found
>= 4) y
= 2; else y
= 2;
217 //rdr_log(reader, "the restart atempts in deprecated is %u", y);
219 for(deprecated
= reader
->deprecated
; deprecated
< y
; deprecated
++)
221 if(!reader_activate_card(reader
, &atr
, deprecated
)) { break; }
223 ret
= reader_get_cardsystem(reader
, &atr
);
228 { rdr_log(reader
, "Normal mode failed, reverting to Deprecated Mode"); }
234 rdr_log(reader
,"THIS WAS A SUCCESSFUL START ATTEMPT No %u out of max allotted of %u", (i
+ 1), j
);
239 rdr_log(reader
, "THIS WAS A FAILED START ATTEMPT No %u out of max allotted of %u", (i
+ 1), j
);
245 reader
->card_status
= CARD_FAILURE
;
246 rdr_log(reader
, "card initializing error");
247 ICC_Async_DisplayMsg(reader
, "AER");
248 led_status_card_activation_error();
252 cardreader_get_card_info(reader
);
253 reader
->card_status
= CARD_INSERTED
;
254 do_emm_from_file(reader
);
255 ICC_Async_DisplayMsg(reader
, "AOK");
257 gbx_local_card_stat(LOCALCARDUP
, reader
->caid
); // local card up
264 static int32_t cardreader_device_init(struct s_reader
*reader
)
266 int32_t rc
= -1; // FIXME
267 if(ICC_Async_Device_Init(reader
))
268 { rdr_log(reader
, "Cannot open device: %s", reader
->device
); }
271 return ((rc
!= OK
) ? 2 : 0); // exit code 2 means keep retrying, exit code 0 means all OK
274 int32_t cardreader_do_checkhealth(struct s_reader
*reader
)
276 struct s_client
*cl
= reader
->client
;
277 if(reader_card_inserted(reader
))
279 if(reader
->card_status
== NO_CARD
|| reader
->card_status
== UNKNOWN
)
281 rdr_log(reader
, "card detected");
282 led_status_card_detected();
283 reader
->card_status
= CARD_NEED_INIT
;
284 add_job(cl
, ACTION_READER_RESET
, NULL
, 0);
289 rdr_log_dbg(reader
, D_READER
, "%s: !reader_card_inserted", __func__
);
290 if(reader
->card_status
== CARD_INSERTED
|| reader
->card_status
== CARD_NEED_INIT
)
292 rdr_log(reader
, "card ejected");
293 reader_nullcard(reader
);
294 if(reader
->csystem
&& reader
->csystem
->card_done
)
295 reader
->csystem
->card_done(reader
);
296 NULLFREE(reader
->csystem_data
);
303 led_status_card_ejected();
305 reader
->card_status
= NO_CARD
;
306 gbx_local_card_stat(LOCALCARDEJECTED
, reader
->caid
);
309 reader
->card_status
= NO_CARD
;
311 rdr_log_dbg(reader
, D_READER
, "%s: reader->card_status = %d, ret = %d", __func__
,
312 reader
->card_status
, reader
->card_status
== CARD_INSERTED
);
314 return reader
->card_status
== CARD_INSERTED
;
317 // Check for card inserted or card removed on pysical reader
318 void cardreader_checkhealth(struct s_client
*cl
, struct s_reader
*rdr
)
320 if(!rdr
|| !rdr
->enable
|| !rdr
->active
)
322 add_job(cl
, ACTION_READER_CHECK_HEALTH
, NULL
, 0);
325 void cardreader_reset(struct s_client
*cl
)
327 add_job(cl
, ACTION_READER_RESET
, NULL
, 0);
330 void cardreader_init_locks(void)
332 ICC_Async_Init_Locks();
335 bool cardreader_init(struct s_reader
*reader
)
337 struct s_client
*client
= reader
->client
;
340 set_localhost_ip(&client
->ip
);
342 while((cardreader_device_init(reader
) == 2) && i
< 10)
345 if(!ll_contains(configured_readers
, reader
) || !is_valid_client(client
) || reader
->enable
!= 1)
352 reader
->card_status
= READER_DEVICE_ERROR
;
353 cardreader_close(reader
);
359 if(reader
->typ
== R_INTERNAL
)
361 if(boxtype_is("dm8000") || boxtype_is("dm800") || boxtype_is("dm800se"))
362 {reader
->cardmhz
= 2700;}
364 if(boxtype_is("dm500") || boxtype_is("dm600pvr"))
365 {reader
->cardmhz
= 3150;}
367 if(boxtype_is("dm7025"))
368 {reader
->cardmhz
= 8300;}
370 if((!strncmp(boxtype_get(), "vu", 2 ))||(boxtype_is("ini-8000am")))
371 {reader
->cardmhz
= 2700; reader
->mhz
= 450;} // only one speed for vu+ and Atemio Nemesis due to usage of TDA8024
374 if((reader
->cardmhz
> 2000) && (reader
->typ
!= R_SMART
))
376 rdr_log(reader
, "Reader initialized (device=%s, detect=%s%s, pll max=%.2f MHz, wanted mhz=%.2f MHz)",
378 reader
->detect
& 0x80 ? "!" : "",
379 RDR_CD_TXT
[reader
->detect
& 0x7f],
380 (float)reader
->cardmhz
/ 100,
381 (float)reader
->mhz
/ 100);
382 rdr_log(reader
,"Reader sci internal, detected box type: %s", boxtype_get());
386 if (reader
->typ
== R_SMART
|| is_smargo_reader(reader
))
388 rdr_log_dbg(reader
, D_IFD
, "clocking for smartreader with smartreader or smargo protocol");
389 if (reader
->cardmhz
>= 2000) reader
->cardmhz
= 369; else
390 if (reader
->cardmhz
>= 1600) reader
->cardmhz
= 1600; else
391 if (reader
->cardmhz
>= 1200) reader
->cardmhz
= 1200; else
392 if (reader
->cardmhz
>= 961) reader
->cardmhz
= 961; else
393 if (reader
->cardmhz
>= 800) reader
->cardmhz
= 800; else
394 if (reader
->cardmhz
>= 686) reader
->cardmhz
= 686; else
395 if (reader
->cardmhz
>= 600) reader
->cardmhz
= 600; else
396 if (reader
->cardmhz
>= 534) reader
->cardmhz
= 534; else
397 if (reader
->cardmhz
>= 480) reader
->cardmhz
= 480; else
398 if (reader
->cardmhz
>= 436) reader
->cardmhz
= 436; else
399 if (reader
->cardmhz
>= 400) reader
->cardmhz
= 400; else
400 if (reader
->cardmhz
>= 369) reader
->cardmhz
= 369; else
401 if (reader
->cardmhz
== 357) reader
->cardmhz
= 369; else // 357 not a default smartreader setting
402 if (reader
->cardmhz
>= 343) reader
->cardmhz
= 343; else
403 reader
->cardmhz
= 320;
405 if (reader
->mhz
>= 1600) reader
->mhz
= 1600; else
406 if (reader
->mhz
>= 1200) reader
->mhz
= 1200; else
407 if (reader
->mhz
>= 961) reader
->mhz
= 961; else
408 if (reader
->mhz
>= 900) reader
->mhz
= 900; else
409 if (reader
->mhz
>= 800) reader
->mhz
= 800; else
410 if (reader
->mhz
>= 686) reader
->mhz
= 686; else
411 if (reader
->mhz
>= 600) reader
->mhz
= 600; else
412 if (reader
->mhz
>= 534) reader
->mhz
= 534; else
413 if (reader
->mhz
>= 480) reader
->mhz
= 480; else
414 if (reader
->mhz
>= 436) reader
->mhz
= 436; else
415 if (reader
->mhz
>= 400) reader
->mhz
= 369; else
416 if (reader
->mhz
>= 369) reader
->mhz
= 369; else
417 if (reader
->mhz
== 357) reader
->mhz
= 369; else // 357 not a default smartreader setting
418 if (reader
->mhz
>= 343) reader
->mhz
= 343; else
422 if ((reader
->typ
== R_SMART
|| is_smargo_reader(reader
)) && reader
->autospeed
== 1)
424 rdr_log(reader
, "Reader initialized (device=%s, detect=%s%s, mhz= AUTO, cardmhz=%d)",
426 reader
->detect
& 0x80 ? "!" : "",
427 RDR_CD_TXT
[reader
->detect
& 0x7f],
432 rdr_log(reader
, "Reader initialized (device=%s, detect=%s%s, mhz=%d, cardmhz=%d)",
434 reader
->detect
& 0x80 ? "!" : "",
435 RDR_CD_TXT
[reader
->detect
& 0x7f],
439 if (reader
->typ
== R_INTERNAL
&& !(reader
->cardmhz
> 2000))
440 rdr_log(reader
,"Reader sci internal, detected box type: %s", boxtype_get());
447 void cardreader_close(struct s_reader
*reader
)
449 ICC_Async_Close(reader
);
452 void reader_post_process(struct s_reader
*reader
)
454 // some systems eg. nagra2/3 needs post process after receiving cw from card
455 // To save ECM/CW time we added this function after writing ecm answer
456 if(reader
->csystem_active
&& reader
->csystem
&& reader
->csystem
->post_process
)
458 reader
->csystem
->post_process(reader
);
462 int32_t cardreader_do_ecm(struct s_reader
*reader
, ECM_REQUEST
*er
, struct s_ecm_answer
*ea
)
465 if((rc
= cardreader_do_checkhealth(reader
)))
467 rdr_log_dbg(reader
, D_READER
, "%s: cardreader_do_checkhealth returned rc=%d", __func__
, rc
);
468 struct s_client
*cl
= reader
->client
;
471 cl
->last_srvid
= er
->srvid
;
472 cl
->last_caid
= er
->caid
;
473 cl
->last_provid
= er
->prid
;
474 cl
->last
= time((time_t *)0);
477 if(reader
->csystem_active
&& reader
->csystem
&& reader
->csystem
->do_ecm
)
479 rc
= reader
->csystem
->do_ecm(reader
, er
, ea
);
480 rdr_log_dbg(reader
, D_READER
, "%s: after csystem->do_ecm rc=%d", __func__
, rc
);
485 rdr_log_dbg(reader
, D_READER
, "%s: ret rc=%d", __func__
, rc
);
489 int32_t cardreader_do_emm(struct s_reader
*reader
, EMM_PACKET
*ep
)
493 // check health does not work with new card status check but is actually not needed for emm.
494 if(reader
->typ
== R_SMART
)
500 rc
= cardreader_do_checkhealth(reader
);
505 if((1 << (ep
->emm
[0] % 0x80)) & reader
->b_nano
)
508 if(reader
->csystem_active
&& reader
->csystem
&& reader
->csystem
->do_emm
)
509 { rc
= reader
->csystem
->do_emm(reader
, ep
); }
514 if(rc
> 0) { cs_ftime(&reader
->emm_last
); } // last time emm written is now!
518 int32_t cardreader_do_rawcmd(struct s_reader
*reader
, CMD_PACKET
*cp
)
521 rc
= -9; // means no dedicated support by csystem
522 if(reader
->csystem_active
&& reader
->csystem
&& reader
->csystem
->do_rawcmd
)
524 rc
= reader
->csystem
->do_rawcmd(reader
, cp
);
529 void cardreader_process_ecm(struct s_reader
*reader
, struct s_client
*cl
, ECM_REQUEST
*er
)
531 struct timeb tps
, tpe
;
532 struct s_ecm_answer ea
;
533 memset(&ea
, 0, sizeof(struct s_ecm_answer
));
535 #ifdef WITH_EXTENDED_CW
536 // Correct CSA mode is CBC - default to that instead
537 ea
.cw_ex
.algo_mode
= CW_ALGO_MODE_CBC
;
541 int32_t rc
= cardreader_do_ecm(reader
, er
, &ea
);
544 rdr_log_dbg(reader
, D_READER
, "%s: cardreader_do_ecm returned rc=%d (ERROR=%d)", __func__
, rc
, ERROR
);
546 ea
.rc
= E_FOUND
; // default assume found
547 ea
.rcEx
= 0; // no special flag
551 char buf
[CS_SERVICENAME_SIZE
];
552 rdr_log_dbg(reader
, D_READER
, "Error processing ecm for caid %04X, provid %06X, srvid %04X, servicename: %s",
553 er
->caid
, er
->prid
, er
->srvid
, get_servicename(cl
, er
->srvid
, er
->prid
, er
->caid
, buf
, sizeof(buf
)));
556 ICC_Async_DisplayMsg(reader
, "Eer");
561 char buf
[CS_SERVICENAME_SIZE
];
562 rdr_log_dbg(reader
, D_READER
, "Error processing ecm for caid %04X, provid %06X, srvid %04X, servicename: %s",
563 er
->caid
, er
->prid
, er
->srvid
, get_servicename(cl
, er
->srvid
, er
->prid
, er
->caid
, buf
, sizeof(buf
)));
565 ea
.rcEx
= E2_WRONG_CHKSUM
; // flag it as wrong checksum
566 memcpy(ea
.msglog
, "Invalid ecm type for card", 25);
568 #ifdef CS_CACHEEX_AIO
569 er
->localgenerated
= 1;
571 write_ecm_answer(reader
, er
, ea
.rc
, ea
.rcEx
, ea
.cw
, ea
.msglog
, ea
.tier
, &ea
.cw_ex
);
573 cl
->lastecm
= time((time_t *)0);
575 if(cs_dblevel
& D_READER
)
578 cs_hexdump(0, er
->ecmd5
, 16, ecmd5
, sizeof(ecmd5
));
580 rdr_log_dbg(reader
, D_READER
, "ecm hash: %s real time: %"PRId64
" ms", ecmd5
, comp_timeb(&tpe
, &tps
));
583 reader_post_process(reader
);