1 | #include "globals.h"
|
---|
2 | #ifdef READER_VIDEOGUARD
|
---|
3 | #include "reader-common.h"
|
---|
4 | #include "reader-videoguard-common.h"
|
---|
5 |
|
---|
6 |
|
---|
7 | static int32_t vg1_do_cmd(struct s_reader *reader, const unsigned char *ins, const unsigned char *txbuff, unsigned char *rxbuff, unsigned char *cta_res)
|
---|
8 | {
|
---|
9 | uint16_t cta_lr;
|
---|
10 | unsigned char ins2[5];
|
---|
11 | memcpy(ins2, ins, 5);
|
---|
12 | unsigned char len = 0;
|
---|
13 | len = ins2[4];
|
---|
14 |
|
---|
15 |
|
---|
16 |
|
---|
17 | if (txbuff == NULL) {
|
---|
18 | if (!write_cmd_vg(ins2, NULL) || !status_ok(cta_res + len)) {
|
---|
19 | return -1;
|
---|
20 | }
|
---|
21 | if(rxbuff != NULL){
|
---|
22 | memcpy(rxbuff, ins2, 5);
|
---|
23 | memcpy(rxbuff + 5, cta_res, len);
|
---|
24 | memcpy(rxbuff + 5 + len, cta_res + len, 2);
|
---|
25 | }
|
---|
26 | } else {
|
---|
27 | if (!write_cmd_vg(ins2, (uchar *) txbuff) || !status_ok(cta_res)) {
|
---|
28 | return -2;
|
---|
29 | }
|
---|
30 | if(rxbuff != NULL){
|
---|
31 | memcpy(rxbuff, ins2, 5);
|
---|
32 | memcpy(rxbuff + 5, txbuff, len);
|
---|
33 | memcpy(rxbuff + 5 + len, cta_res, 2);
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | return len;
|
---|
38 | }
|
---|
39 |
|
---|
40 | static void read_tiers(struct s_reader *reader)
|
---|
41 | {
|
---|
42 | def_resp;
|
---|
43 | // const unsigned char ins2a[5] = { 0x48, 0x2a, 0x00, 0x00, 0x00 };
|
---|
44 | int32_t l;
|
---|
45 |
|
---|
46 | // return; // Not working at present so just do nothing
|
---|
47 |
|
---|
48 | // l = vg1_do_cmd(reader, ins2a, NULL, NULL, cta_res);
|
---|
49 | // if (l < 0 || !status_ok(cta_res + l))
|
---|
50 | // {
|
---|
51 | // return;
|
---|
52 | // }
|
---|
53 | unsigned char ins76[5] = { 0x48, 0x76, 0x00, 0x00, 0x00 };
|
---|
54 | ins76[3] = 0x7f;
|
---|
55 | ins76[4] = 2;
|
---|
56 | if (!write_cmd_vg(ins76, NULL) || !status_ok(cta_res + 2)) {
|
---|
57 | return;
|
---|
58 | }
|
---|
59 | ins76[3] = 0;
|
---|
60 | ins76[4] = 0x0a;
|
---|
61 | int32_t num = cta_res[1];
|
---|
62 | int32_t i;
|
---|
63 |
|
---|
64 | cs_clear_entitlement(reader); //reset the entitlements
|
---|
65 |
|
---|
66 | for (i = 0; i < num; i++) {
|
---|
67 | ins76[2] = i;
|
---|
68 | l = vg1_do_cmd(reader, ins76, NULL, NULL, cta_res);
|
---|
69 | if (l < 0 || !status_ok(cta_res + l)) {
|
---|
70 | return;
|
---|
71 | }
|
---|
72 | if (cta_res[2] == 0 && cta_res[3] == 0) {
|
---|
73 | break;
|
---|
74 | }
|
---|
75 | int32_t y, m, d, H, M, S;
|
---|
76 | rev_date_calc(&cta_res[4], &y, &m, &d, &H, &M, &S, reader->card_baseyear);
|
---|
77 | uint16_t tier_id = (cta_res[2] << 8) | cta_res[3];
|
---|
78 |
|
---|
79 |
|
---|
80 | // add entitlements to list
|
---|
81 | struct tm timeinfo;
|
---|
82 | memset(&timeinfo, 0, sizeof(struct tm));
|
---|
83 | timeinfo.tm_year = y - 1900; //tm year starts with 1900
|
---|
84 | timeinfo.tm_mon = m - 1; //tm month starts with 0
|
---|
85 | timeinfo.tm_mday = d;
|
---|
86 | cs_add_entitlement(reader, reader->caid, b2ll(4, reader->prid[0]), tier_id, 0, 0, mktime(&timeinfo), 4);
|
---|
87 |
|
---|
88 | char tiername[83];
|
---|
89 | cs_ri_log(reader, "[videoguard1-reader] tier: %04x, expiry date: %04d/%02d/%02d-%02d:%02d:%02d %s", tier_id, y, m, d, H, M, S, get_tiername(tier_id, reader->caid, tiername));
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | static int32_t videoguard1_card_init(struct s_reader *reader, ATR *newatr)
|
---|
94 | {
|
---|
95 |
|
---|
96 | get_hist;
|
---|
97 | /* 40 B0 09 4A 50 01 4E 5A */
|
---|
98 | if ((hist_size < 7) || (hist[1] != 0xB0) || (hist[3] != 0x4A) || (hist[4] != 0x50)) {
|
---|
99 | return ERROR;
|
---|
100 | }
|
---|
101 |
|
---|
102 | get_atr;
|
---|
103 | def_resp;
|
---|
104 |
|
---|
105 | /* set information on the card stored in reader-videoguard-common.c */
|
---|
106 | set_known_card_info(reader,atr,&atr_size);
|
---|
107 |
|
---|
108 | if((reader->ndsversion != NDS1) && ((reader->card_system_version != NDS1) || (reader->ndsversion != NDSAUTO))) {
|
---|
109 | /* known ATR and not NDS1
|
---|
110 | or unknown ATR and not forced to NDS1
|
---|
111 | or known NDS1 ATR and forced to another NDS version
|
---|
112 | ... probably not NDS1 */
|
---|
113 | return ERROR;
|
---|
114 | }
|
---|
115 |
|
---|
116 | cs_ri_log(reader, "[videoguard1-reader] type: %s, baseyear: %i", reader->card_desc, reader->card_baseyear);
|
---|
117 | if(reader->ndsversion == NDS1){
|
---|
118 | cs_log("[videoguard1-reader] forced to NDS1+");
|
---|
119 | }
|
---|
120 |
|
---|
121 | /* NDS1 Class 48 only cards only need a very basic initialisation
|
---|
122 | NDS1 Class 48 only cards do not respond to vg1_do_cmd(ins7416)
|
---|
123 | nor do they return list of valid command therefore do not even try
|
---|
124 | NDS1 Class 48 only cards need to be told the length as (48, ins, 00, 80, 01)
|
---|
125 | does not return the length */
|
---|
126 |
|
---|
127 | int32_t l = 0;
|
---|
128 | unsigned char buff[256];
|
---|
129 |
|
---|
130 | /* Try to get the boxid from the card, even if BoxID specified in the config file
|
---|
131 | also used to check if it is an NDS1 card as the returned information will
|
---|
132 | not be encrypted if it is an NDS1 card */
|
---|
133 |
|
---|
134 | static const unsigned char ins36[5] = { 0x48, 0x36, 0x00, 0x00, 0x90 };
|
---|
135 | unsigned char boxID[4];
|
---|
136 | int32_t boxidOK = 0;
|
---|
137 | l = vg1_do_cmd(reader, ins36, NULL, buff, cta_res);
|
---|
138 | if (buff[7] > 0x0F) {
|
---|
139 | cs_log("[videoguard1-reader] class48 ins36: encrypted - therefore not an NDS1 card");
|
---|
140 | return ERROR;
|
---|
141 | } else {
|
---|
142 | /* skipping the initial fixed fields: cmdecho (4) + length (1) + encr/rev++ (4) */
|
---|
143 | int32_t i = 9;
|
---|
144 | int32_t gotUA = 0;
|
---|
145 | while (i < l) {
|
---|
146 | if (!gotUA && buff[i] < 0xF0) { /* then we guess that the next 4 bytes is the UA */
|
---|
147 | gotUA = 1;
|
---|
148 | i += 4;
|
---|
149 | } else {
|
---|
150 | switch (buff[i]) { /* object length vary depending on type */
|
---|
151 | case 0x00: /* padding */
|
---|
152 | {
|
---|
153 | i += 1;
|
---|
154 | break;
|
---|
155 | }
|
---|
156 | case 0xEF: /* card status */
|
---|
157 | {
|
---|
158 | i += 3;
|
---|
159 | break;
|
---|
160 | }
|
---|
161 | case 0xD1:
|
---|
162 | {
|
---|
163 | i += 4;
|
---|
164 | break;
|
---|
165 | }
|
---|
166 | case 0xDF: /* next server contact */
|
---|
167 | {
|
---|
168 | i += 5;
|
---|
169 | break;
|
---|
170 | }
|
---|
171 | case 0xF3: /* boxID */
|
---|
172 | {
|
---|
173 | memcpy(&boxID, &buff[i + 1], sizeof(boxID));
|
---|
174 | boxidOK = 1;
|
---|
175 | i += 5;
|
---|
176 | break;
|
---|
177 | }
|
---|
178 | case 0xF6:
|
---|
179 | {
|
---|
180 | i += 6;
|
---|
181 | break;
|
---|
182 | }
|
---|
183 | case 0xFC: /* No idea seems to with with NDS1 */
|
---|
184 | {
|
---|
185 | i += 14;
|
---|
186 | break;
|
---|
187 | }
|
---|
188 | case 0x01: /* date & time */
|
---|
189 | {
|
---|
190 | i += 7;
|
---|
191 | break;
|
---|
192 | }
|
---|
193 | case 0xFA:
|
---|
194 | {
|
---|
195 | i += 9;
|
---|
196 | break;
|
---|
197 | }
|
---|
198 | case 0x5E:
|
---|
199 | case 0x67: /* signature */
|
---|
200 | case 0xDE:
|
---|
201 | case 0xE2:
|
---|
202 | case 0xE9: /* tier dates */
|
---|
203 | case 0xF8: /* Old PPV Event Record */
|
---|
204 | case 0xFD:
|
---|
205 | {
|
---|
206 | i += buff[i + 1] + 2; /* skip length + 2 bytes (type and length) */
|
---|
207 | break;
|
---|
208 | }
|
---|
209 | default: /* default to assume a length byte */
|
---|
210 | {
|
---|
211 | cs_log("[videoguard1-reader] class48 ins36: returned unknown type=0x%02X - parsing may fail", buff[i]);
|
---|
212 | i += buff[i + 1] + 2;
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | // cs_log("[videguard1nz-reader] calculated BoxID: %02X%02X%02X%02X", boxID[0], boxID[1], boxID[2], boxID[3]);
|
---|
220 |
|
---|
221 | /* the boxid is specified in the config */
|
---|
222 | if (reader->boxid > 0) {
|
---|
223 | int32_t i;
|
---|
224 | for (i = 0; i < 4; i++) {
|
---|
225 | boxID[i] = (reader->boxid >> (8 * (3 - i))) % 0x100;
|
---|
226 | }
|
---|
227 | // cs_log("[videguard1nz-reader] config BoxID: %02X%02X%02X%02X", boxID[0], boxID[1], boxID[2], boxID[3]);
|
---|
228 | }
|
---|
229 |
|
---|
230 | if (!boxidOK) {
|
---|
231 | cs_log("[videoguard1-reader] no boxID available");
|
---|
232 | return ERROR;
|
---|
233 | }
|
---|
234 |
|
---|
235 | // Send BoxID
|
---|
236 | static const unsigned char ins4C[5] = { 0x48, 0x4C, 0x00, 0x00, 0x09 };
|
---|
237 | unsigned char payload4C[9] = { 0, 0, 0, 0, 3, 0, 0, 0, 4 };
|
---|
238 | memcpy(payload4C, boxID, 4);
|
---|
239 | if (!write_cmd_vg(ins4C, payload4C) || !status_ok(cta_res + l)) {
|
---|
240 | cs_log("[videoguard1-reader] class48 ins4C: sending boxid failed");
|
---|
241 | return ERROR;
|
---|
242 | }
|
---|
243 |
|
---|
244 | static const unsigned char ins58[5] = { 0x48, 0x58, 0x00, 0x00, 0x17 };
|
---|
245 | l = vg1_do_cmd(reader, ins58, NULL, buff, cta_res);
|
---|
246 | if (l < 0) {
|
---|
247 | cs_log("[videoguard1-reader] class48 ins58: failed");
|
---|
248 | return ERROR;
|
---|
249 | }
|
---|
250 |
|
---|
251 | memset(reader->hexserial, 0, 8);
|
---|
252 | memcpy(reader->hexserial + 2, cta_res + 1, 4);
|
---|
253 | memcpy(reader->sa, cta_res + 1, 3);
|
---|
254 | // reader->caid = cta_res[24] * 0x100 + cta_res[25];
|
---|
255 | /* Force caid until can figure out how to get it */
|
---|
256 | reader->caid = 0x9 * 0x100 + 0x69;
|
---|
257 |
|
---|
258 | /* we have one provider, 0x0000 */
|
---|
259 | reader->nprov = 1;
|
---|
260 | memset(reader->prid, 0x00, sizeof(reader->prid));
|
---|
261 |
|
---|
262 | cs_ri_log(reader,
|
---|
263 | "[videoguard1-reader] type: VideoGuard, caid: %04X, serial: %02X%02X%02X%02X, BoxID: %02X%02X%02X%02X",
|
---|
264 | reader->caid, reader->hexserial[2], reader->hexserial[3], reader->hexserial[4], reader->hexserial[5], boxID[0], boxID[1], boxID[2], boxID[3]);
|
---|
265 | cs_log("[videoguard1-reader] ready for requests - this is in testing please send -d 255 logs");
|
---|
266 |
|
---|
267 | return OK;
|
---|
268 | }
|
---|
269 |
|
---|
270 | static int32_t videoguard1_do_ecm(struct s_reader * reader, const ECM_REQUEST *er, struct s_ecm_answer *ea)
|
---|
271 | {
|
---|
272 | unsigned char cta_res[CTA_RES_LEN];
|
---|
273 | unsigned char ins40[5] = { 0x48, 0x40, 0x00, 0x80, 0xFF };
|
---|
274 | static const unsigned char ins54[5] = { 0x48, 0x54, 0x00, 0x00, 0x0D };
|
---|
275 | int32_t posECMpart2 = er->ecm[6] + 7;
|
---|
276 | int32_t lenECMpart2 = er->ecm[posECMpart2];
|
---|
277 | unsigned char tbuff[264];
|
---|
278 | unsigned char rbuff[264];
|
---|
279 | memcpy(&tbuff[0], &(er->ecm[posECMpart2 + 1]), lenECMpart2);
|
---|
280 | ins40[4] = lenECMpart2;
|
---|
281 | int32_t l;
|
---|
282 | l = vg1_do_cmd(reader, ins40, tbuff, NULL, cta_res);
|
---|
283 | if (l > 0 && status_ok(cta_res)) {
|
---|
284 | l = vg1_do_cmd(reader, ins54, NULL, rbuff, cta_res);
|
---|
285 | if (l > 0 && status_ok(cta_res + l)) {
|
---|
286 | if (!cw_is_valid(rbuff+5)) //sky cards report 90 00 = ok but send cw = 00 when channel not subscribed
|
---|
287 | {
|
---|
288 | cs_log("[videoguard1-reader] class48 ins54 status 90 00 but cw=00 -> channel not subscribed");
|
---|
289 | return ERROR;
|
---|
290 | }
|
---|
291 |
|
---|
292 | if(er->ecm[0]&1) {
|
---|
293 | memset(ea->cw+0, 0, 8);
|
---|
294 | memcpy(ea->cw+8, rbuff + 5, 8);
|
---|
295 | } else {
|
---|
296 | memcpy(ea->cw+0, rbuff + 5, 8);
|
---|
297 | memset(ea->cw+8, 0, 8);
|
---|
298 | }
|
---|
299 | return OK;
|
---|
300 | }
|
---|
301 | }
|
---|
302 | cs_log("[videoguard1-reader] class48 ins54 (%d) status not ok %02x %02x", l, cta_res[0], cta_res[1]);
|
---|
303 | return ERROR;
|
---|
304 | }
|
---|
305 |
|
---|
306 | static int32_t videoguard1_do_emm(struct s_reader *reader, EMM_PACKET * ep)
|
---|
307 | {
|
---|
308 | return videoguard_do_emm(reader, ep, 0x48, read_tiers, vg1_do_cmd);
|
---|
309 | }
|
---|
310 |
|
---|
311 | static int32_t videoguard1_card_info(struct s_reader *reader)
|
---|
312 | {
|
---|
313 | /* info is displayed in init, or when processing info */
|
---|
314 | cs_log("[videoguard1-reader] card detected");
|
---|
315 | cs_log("[videoguard1-reader] type: %s", reader->card_desc);
|
---|
316 | read_tiers(reader);
|
---|
317 | return OK;
|
---|
318 | }
|
---|
319 |
|
---|
320 | void reader_videoguard1(struct s_cardsystem *ph)
|
---|
321 | {
|
---|
322 | ph->do_emm=videoguard1_do_emm;
|
---|
323 | ph->do_ecm=videoguard1_do_ecm;
|
---|
324 | ph->card_info=videoguard1_card_info;
|
---|
325 | ph->card_init=videoguard1_card_init;
|
---|
326 | ph->get_emm_type=videoguard_get_emm_type;
|
---|
327 | ph->get_emm_filter=videoguard_get_emm_filter;
|
---|
328 | ph->caids[0]=0x09;
|
---|
329 | ph->desc="videoguard1";
|
---|
330 | }
|
---|
331 | #endif
|
---|