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