This source file includes following definitions.
- StartDecryption
- DecryptBuf
- pof_handle_data
- pof_write_buffer
- pof_write_open
- pof_write_close
- EvalSysrTokData
1
2
3
4
5
6
7
8
9
10
11
12
13
14 #include <linux/vmalloc.h>
15 #include <linux/slab.h>
16 #include <linux/uaccess.h>
17
18 #include "hysdn_defs.h"
19 #include "hysdn_pof.h"
20
21
22
23
24 #define POF_READ_FILE_HEAD 0
25 #define POF_READ_TAG_HEAD 1
26 #define POF_READ_TAG_DATA 2
27
28
29
30
31
32 struct boot_data {
33 unsigned short Cryptor;
34 unsigned short Nrecs;
35 unsigned char pof_state;
36 unsigned char is_crypted;
37 int BufSize;
38 int last_error;
39 unsigned short pof_recid;
40 unsigned long pof_reclen;
41 unsigned long pof_recoffset;
42 union {
43 unsigned char BootBuf[BOOT_BUF_SIZE];
44 tPofRecHdr PofRecHdr;
45 tPofFileHdr PofFileHdr;
46 tPofTimeStamp PofTime;
47 } buf;
48 };
49
50
51
52
53
54
55
56 static void
57 StartDecryption(struct boot_data *boot)
58 {
59 boot->Cryptor = CRYPT_STARTTERM;
60 }
61
62
63
64
65
66
67
68
69 static void
70 DecryptBuf(struct boot_data *boot, int cnt)
71 {
72 unsigned char *bufp = boot->buf.BootBuf;
73
74 while (cnt--) {
75 boot->Cryptor = (boot->Cryptor >> 1) ^ ((boot->Cryptor & 1U) ? CRYPT_FEEDTERM : 0);
76 *bufp++ ^= (unsigned char)boot->Cryptor;
77 }
78 }
79
80
81
82
83
84 static int
85 pof_handle_data(hysdn_card *card, int datlen)
86 {
87 struct boot_data *boot = card->boot;
88 long l;
89 unsigned char *imgp;
90 int img_len;
91
92
93 switch (boot->pof_recid) {
94
95 case TAG_TIMESTMP:
96 if (card->debug_flags & LOG_POF_RECORD)
97 hysdn_addlog(card, "POF created %s", boot->buf.PofTime.DateTimeText);
98 break;
99
100 case TAG_CBOOTDTA:
101 DecryptBuf(boot, datlen);
102
103 case TAG_BOOTDTA:
104 if (card->debug_flags & LOG_POF_RECORD)
105 hysdn_addlog(card, "POF got %s len=%d offs=0x%lx",
106 (boot->pof_recid == TAG_CBOOTDTA) ? "CBOOTDATA" : "BOOTDTA",
107 datlen, boot->pof_recoffset);
108
109 if (boot->pof_reclen != POF_BOOT_LOADER_TOTAL_SIZE) {
110 boot->last_error = EPOF_BAD_IMG_SIZE;
111 return (boot->last_error);
112 }
113 imgp = boot->buf.BootBuf;
114 img_len = datlen;
115
116 l = POF_BOOT_LOADER_OFF_IN_PAGE -
117 (boot->pof_recoffset & (POF_BOOT_LOADER_PAGE_SIZE - 1));
118 if (l > 0) {
119
120 imgp += l;
121 img_len -= l;
122 }
123
124
125
126
127
128
129
130 if (img_len > 0) {
131
132 if ((boot->last_error =
133 card->writebootimg(card, imgp,
134 (boot->pof_recoffset > POF_BOOT_LOADER_PAGE_SIZE) ? 2 : 0)) < 0)
135 return (boot->last_error);
136 }
137 break;
138
139 case TAG_CABSDATA:
140 DecryptBuf(boot, datlen);
141
142 case TAG_ABSDATA:
143 if (card->debug_flags & LOG_POF_RECORD)
144 hysdn_addlog(card, "POF got %s len=%d offs=0x%lx",
145 (boot->pof_recid == TAG_CABSDATA) ? "CABSDATA" : "ABSDATA",
146 datlen, boot->pof_recoffset);
147
148 if ((boot->last_error = card->writebootseq(card, boot->buf.BootBuf, datlen)) < 0)
149 return (boot->last_error);
150
151 if (boot->pof_recoffset + datlen >= boot->pof_reclen)
152 return (card->waitpofready(card));
153
154 break;
155
156 default:
157 if (card->debug_flags & LOG_POF_RECORD)
158 hysdn_addlog(card, "POF got data(id=0x%lx) len=%d offs=0x%lx", boot->pof_recid,
159 datlen, boot->pof_recoffset);
160
161 break;
162 }
163
164 return (0);
165 }
166
167
168
169
170
171
172
173
174
175 int
176 pof_write_buffer(hysdn_card *card, int datlen)
177 {
178 struct boot_data *boot = card->boot;
179
180 if (!boot)
181 return (-EFAULT);
182 if (boot->last_error < 0)
183 return (boot->last_error);
184
185 if (card->debug_flags & LOG_POF_WRITE)
186 hysdn_addlog(card, "POF write: got %d bytes ", datlen);
187
188 switch (boot->pof_state) {
189 case POF_READ_FILE_HEAD:
190 if (card->debug_flags & LOG_POF_WRITE)
191 hysdn_addlog(card, "POF write: checking file header");
192
193 if (datlen != sizeof(tPofFileHdr)) {
194 boot->last_error = -EPOF_INTERNAL;
195 break;
196 }
197 if (boot->buf.PofFileHdr.Magic != TAGFILEMAGIC) {
198 boot->last_error = -EPOF_BAD_MAGIC;
199 break;
200 }
201
202 boot->Nrecs = (unsigned short)(boot->buf.PofFileHdr.N_PofRecs);
203 boot->pof_state = POF_READ_TAG_HEAD;
204 boot->last_error = sizeof(tPofRecHdr);
205 break;
206
207 case POF_READ_TAG_HEAD:
208 if (card->debug_flags & LOG_POF_WRITE)
209 hysdn_addlog(card, "POF write: checking tag header");
210
211 if (datlen != sizeof(tPofRecHdr)) {
212 boot->last_error = -EPOF_INTERNAL;
213 break;
214 }
215 boot->pof_recid = boot->buf.PofRecHdr.PofRecId;
216 boot->pof_reclen = boot->buf.PofRecHdr.PofRecDataLen;
217 boot->pof_recoffset = 0;
218
219 if (card->debug_flags & LOG_POF_RECORD)
220 hysdn_addlog(card, "POF: got record id=0x%lx length=%ld ",
221 boot->pof_recid, boot->pof_reclen);
222
223 boot->pof_state = POF_READ_TAG_DATA;
224 if (boot->pof_reclen < BOOT_BUF_SIZE)
225 boot->last_error = boot->pof_reclen;
226 else
227 boot->last_error = BOOT_BUF_SIZE;
228
229 if (!boot->last_error) {
230 boot->pof_state = POF_READ_TAG_HEAD;
231 boot->last_error = sizeof(tPofRecHdr);
232 }
233 break;
234
235 case POF_READ_TAG_DATA:
236 if (card->debug_flags & LOG_POF_WRITE)
237 hysdn_addlog(card, "POF write: getting tag data");
238
239 if (datlen != boot->last_error) {
240 boot->last_error = -EPOF_INTERNAL;
241 break;
242 }
243 if ((boot->last_error = pof_handle_data(card, datlen)) < 0)
244 return (boot->last_error);
245 boot->pof_recoffset += datlen;
246 if (boot->pof_recoffset >= boot->pof_reclen) {
247 boot->pof_state = POF_READ_TAG_HEAD;
248 boot->last_error = sizeof(tPofRecHdr);
249 } else {
250 if (boot->pof_reclen - boot->pof_recoffset < BOOT_BUF_SIZE)
251 boot->last_error = boot->pof_reclen - boot->pof_recoffset;
252 else
253 boot->last_error = BOOT_BUF_SIZE;
254 }
255 break;
256
257 default:
258 boot->last_error = -EPOF_INTERNAL;
259 break;
260 }
261
262 return (boot->last_error);
263 }
264
265
266
267
268
269
270
271
272 int
273 pof_write_open(hysdn_card *card, unsigned char **bufp)
274 {
275 struct boot_data *boot;
276
277 if (card->boot) {
278 if (card->debug_flags & LOG_POF_OPEN)
279 hysdn_addlog(card, "POF open: already opened for boot");
280 return (-ERR_ALREADY_BOOT);
281 }
282
283 if (!(boot = kzalloc(sizeof(struct boot_data), GFP_KERNEL))) {
284 if (card->debug_flags & LOG_MEM_ERR)
285 hysdn_addlog(card, "POF open: unable to allocate mem");
286 return (-EFAULT);
287 }
288 card->boot = boot;
289 card->state = CARD_STATE_BOOTING;
290
291 card->stopcard(card);
292 if (card->testram(card)) {
293 if (card->debug_flags & LOG_POF_OPEN)
294 hysdn_addlog(card, "POF open: DPRAM test failure");
295 boot->last_error = -ERR_BOARD_DPRAM;
296 card->state = CARD_STATE_BOOTERR;
297 return (boot->last_error);
298 }
299 boot->BufSize = 0;
300 boot->pof_state = POF_READ_FILE_HEAD;
301 StartDecryption(boot);
302
303 if (card->debug_flags & LOG_POF_OPEN)
304 hysdn_addlog(card, "POF open: success");
305
306 *bufp = boot->buf.BootBuf;
307 return (sizeof(tPofFileHdr));
308 }
309
310
311
312
313
314 int
315 pof_write_close(hysdn_card *card)
316 {
317 struct boot_data *boot = card->boot;
318
319 if (!boot)
320 return (-EFAULT);
321
322 card->boot = NULL;
323 kfree(boot);
324
325 if (card->state == CARD_STATE_RUN)
326 card->set_errlog_state(card, 1);
327
328 if (card->debug_flags & LOG_POF_OPEN)
329 hysdn_addlog(card, "POF close: success");
330
331 return (0);
332 }
333
334
335
336
337
338 int
339 EvalSysrTokData(hysdn_card *card, unsigned char *cp, int len)
340 {
341 u_char *p;
342 u_char crc;
343
344 if (card->debug_flags & LOG_POF_RECORD)
345 hysdn_addlog(card, "SysReady Token data length %d", len);
346
347 if (len < 2) {
348 hysdn_addlog(card, "SysReady Token Data to short");
349 return (1);
350 }
351 for (p = cp, crc = 0; p < (cp + len - 2); p++)
352 if ((crc & 0x80))
353 crc = (((u_char) (crc << 1)) + 1) + *p;
354 else
355 crc = ((u_char) (crc << 1)) + *p;
356 crc = ~crc;
357 if (crc != *(cp + len - 1)) {
358 hysdn_addlog(card, "SysReady Token Data invalid CRC");
359 return (1);
360 }
361 len--;
362 while (len > 0) {
363
364 if (*cp == SYSR_TOK_END)
365 return (0);
366
367 if (len < (*(cp + 1) + 2)) {
368 hysdn_addlog(card, "token 0x%x invalid length %d", *cp, *(cp + 1));
369 return (1);
370 }
371 switch (*cp) {
372 case SYSR_TOK_B_CHAN:
373 if (*(cp + 1) != 1)
374 return (1);
375 card->bchans = *(cp + 2);
376 break;
377
378 case SYSR_TOK_FAX_CHAN:
379 if (*(cp + 1) != 1)
380 return (1);
381 card->faxchans = *(cp + 2);
382 break;
383
384 case SYSR_TOK_MAC_ADDR:
385 if (*(cp + 1) != 6)
386 return (1);
387 memcpy(card->mac_addr, cp + 2, 6);
388 break;
389
390 default:
391 hysdn_addlog(card, "unknown token 0x%02x length %d", *cp, *(cp + 1));
392 break;
393 }
394 len -= (*(cp + 1) + 2);
395 cp += (*(cp + 1) + 2);
396 }
397
398 hysdn_addlog(card, "no end token found");
399 return (1);
400 }