Debian Codegraph @

src/hex2dusb.cc

raw
1 /* hex2dusb - an D-USB packet decompiler
2 * Copyright (C) 2005-2007 Romain Liévin
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <ctype.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24
25 #include "ticables.h"
26 #include "logging.h"
27 #include "internal.h"
28
29 #define ARRAY_SIZE (256)
30
31 /*
32 Format:
33
34 | packet header | data |
35 | | data header | |
36 | size | ty | size | code | data |
37 | | | | | |
38 | 00 00 00 10 | 04 | 00 00 00 0A | 00 01 | 00 03 00 01 00 00 00 00 07 D0 |
39 */
40
41 typedef struct
42 {
43 uint8_t type;
44 uint8_t data_hdr;
45 uint8_t data;
46 const char* name;
47 } Packet;
48
49 static const Packet packets[] =
50 {
51 { 0x01, 0, 4, "Buffer Size Request" },
52 { 0x02, 0, 4, "Buffer Size Allocation" },
53 { 0x03, 1, 6, "Virtual Packet Data with Continuation" },
54 { 0x04, 1, 6, "Virtual Packet Data Final" },
55 { 0x05, 0, 2, "Virtual Packet Data Acknowledgement" },
56 { 0x00, 0, 0, NULL }
57 };
58
59 typedef struct
60 {
61 uint16_t type;
62 const char* name;
63 } Opcode;
64
65 static const Opcode opcodes[] =
66 {
67 { 0x0001, "Ping / Set Mode" },
68 { 0x0002, "Begin OS Transfer" },
69 { 0x0003, "Acknowledgement of OS Transfer" },
70 { 0x0004, "OS Header" },
71 { 0x0005, "OS Data" },
72 { 0x0006, "Acknowledgement of EOT" },
73 { 0x0007, "Parameter Request"},
74 { 0x0008, "Parameter Data"},
75 { 0x0009, "Request Directory Listing" },
76 { 0x000A, "Variable Header" },
77 { 0x000B, "Request to Send" },
78 { 0x000C, "Request Variable" },
79 { 0x000D, "Variable Contents" },
80 { 0x000E, "Parameter Set"},
81 { 0x0010, "Modify Variable"},
82 { 0x0011, "Remote Control"},
83 { 0x0012, "Acknowledgement of Mode Setting"},
84 { 0xAA00, "Acknowledgement of Data"},
85 { 0xBB00, "Acknowledgement of Parameter Request"},
86 { 0xDD00, "End of Transmission"},
87 { 0xEE00, "Error"},
88 { 0, NULL }
89 };
90
91 /* */
92
93 /*static int is_a_packet(uint8_t id)
94 {
95 int i;
96
97 for (i=0; packets[i].name; i++)
98 {
99 if (id == packets[i].type)
100 {
101 break;
102 }
103 }
104
105 return i;
106 }*/
107
108 static const char* name_of_packet(uint8_t id)
109 {
110 int i;
111
112 for (i=0; packets[i].name; i++)
113 {
114 if (id == packets[i].type)
115 {
116 return packets[i].name;
117 }
118 }
119
120 return "";
121 }
122
123 /*static int is_a_packet_with_data_header(uint8_t id)
124 {
125 int i;
126
127 for (i=0; packets[i].name; i++)
128 {
129 if (id == packets[i].type)
130 {
131 if (packets[i].data_hdr)
132 {
133 return 1;
134 }
135 }
136 }
137
138 return 0;
139 }*/
140
141 /*static int is_a_opcode(uint16_t id)
142 {
143 int i;
144
145 for (i=0; opcodes[i].name; i++)
146 {
147 if (id == opcodes[i].type)
148 {
149 break;
150 }
151 }
152
153 return i;
154 }*/
155
156 static const char* name_of_data(uint16_t id)
157 {
158 unsigned int i;
159
160 for (i=0; opcodes[i].name != NULL; i++)
161 {
162 if (id == opcodes[i].type)
163 {
164 return opcodes[i].name;
165 }
166 }
167
168 return "unknown";
169 }
170
171 static const char* ep_way(int ep)
172 {
173 if (ep == 0x01)
174 {
175 return "TI>PC";
176 }
177 else if (ep == 0x02)
178 {
179 return "PC>TI";
180 }
181 else
182 {
183 return "XX>XX";
184 }
185 }
186
187 /* */
188
189 static uint8_t pkt_type_found[ARRAY_SIZE] = { 0 };
190 static uint16_t data_code_found[ARRAY_SIZE] = { 0 };
191 static unsigned int ptf=0, dcf=0;
192
193
194 static int add_pkt_type(uint8_t type)
195 {
196 unsigned int i;
197
198 for (i = 0; i < ptf; i++)
199 {
200 if (pkt_type_found[i] == type)
201 {
202 return 0;
203 }
204 }
205
206 if (i < (sizeof(pkt_type_found)/sizeof(pkt_type_found[0])) - 1)
207 {
208 pkt_type_found[++i] = type;
209 }
210 else
211 {
212 static int warn_add_pkt_type = 0;
213 if (!warn_add_pkt_type)
214 {
215 ticables_warning("DUSB protocol interpreter: no room left in pkt_type_found array.");
216 warn_add_pkt_type++;
217 }
218 }
219
220 ptf = i;
221
222 return i;
223 }
224
225 static int add_data_code(uint16_t code)
226 {
227 unsigned int i;
228
229 for (i = 0; i < dcf; i++)
230 {
231 if (data_code_found[i] == code)
232 {
233 return 0;
234 }
235 }
236
237 if (i < (sizeof(data_code_found)/sizeof(data_code_found[0])) - 1)
238 {
239 data_code_found[++i] = code;
240 }
241 else
242 {
243 static int warn_add_data_code = 0;
244 if (!warn_add_data_code)
245 {
246 ticables_warning("DUSB protocol interpreter: no room left in data_code_found array.");
247 warn_add_data_code++;
248 }
249 }
250
251 dcf = i;
252
253 return i;
254 }
255
256 /* */
257
258 static FILE *hex = NULL;
259 static FILE *logfile = NULL;
260
261 static int hex_read(unsigned char *data)
262 {
263 static int idx = 0;
264 int ret;
265 int data2;
266
267 if (feof(hex))
268 {
269 return -1;
270 }
271
272 ret = fscanf(hex, "%02X", &data2);
273 if (ret < 1)
274 {
275 return -1;
276 }
277 *data = data2 & 0xFF;
278 if (fgetc(hex) < 0)
279 {
280 return -1;
281 }
282 idx++;
283
284 if (idx >= 16)
285 {
286 int i;
287
288 idx = 0;
289 for (i = 0; (i < 67-49) && !feof(hex); i++)
290 {
291 if (fgetc(hex) < 0)
292 {
293 return -1;
294 }
295 }
296 }
297
298 return 0;
299 }
300
301 static int dusb_write(int dir, uint8_t data)
302 {
303 static uint8_t array[20];
304 static int i = 0;
305 static unsigned long state = 1;
306 static uint32_t raw_size;
307 static uint8_t raw_type;
308 static uint32_t vtl_size;
309 static uint16_t vtl_type;
310 static int cnt;
311 static int first = 1;
312
313 if (logfile == NULL)
314 {
315 return -1;
316 }
317
318 //printf("<%i %i> ", i, state);
319 array[i++ % 16] = data;
320
321 switch(state) // Finite State Machine
322 {
323 case 1: break;
324 case 2: break;
325 case 3: break;
326 case 4:
327 raw_size = (((uint32_t)(array[0])) << 24) | (((uint32_t)(array[1])) << 16) | (((uint32_t)(array[2])) << 8) | ((uint32_t)(array[3]));
328 fprintf(logfile, "%08x ", (unsigned int)raw_size);
329 break;
330 case 5:
331 raw_type = array[4];
332 fprintf(logfile, "(%02X) ", (unsigned int)raw_type);
333
334 fprintf(logfile, "\t\t\t\t\t\t\t");
335 fprintf(logfile, "| %s: %s\n", ep_way(dir), name_of_packet(raw_type));
336 add_pkt_type(raw_type);
337
338 break;
339 case 6: break;
340 case 7:
341 if (raw_type == 5)
342 {
343 uint16_t tmp = (((uint32_t)(array[5])) << 8) | ((uint32_t)(array[6]));
344 fprintf(logfile, "\t[%04x]\n", tmp);
345 state = 0;
346 }
347 break;
348 case 8: break;
349 case 9:
350 if (raw_type == 1 || raw_type == 2)
351 {
352 uint32_t tmp = (((uint32_t)(array[5])) << 24) | (((uint32_t)(array[6])) << 16) | (((uint32_t)(array[7])) << 8) | ((uint32_t)(array[8]));
353 fprintf(logfile, "\t[%08x]\n", (unsigned int)tmp);
354 state = 0;
355 }
356 else if (first && ((raw_type == 3) || (raw_type == 4)))
357 {
358 vtl_size = (((uint32_t)(array[5])) << 24) | (((uint32_t)(array[6])) << 16) | (((uint32_t)(array[7])) << 8) | ((uint32_t)(array[8]));
359 fprintf(logfile, "\t%08x ", (unsigned int)vtl_size);
360 cnt = 0;
361 first = (raw_type == 3) ? 0 : 1;
362 raw_size -= 6;
363 }
364 else if (!first && ((raw_type == 3) || (raw_type == 4)))
365 {
366 fprintf(logfile, "\t");
367 fprintf(logfile, "%02X %02X %02X ", array[5], array[6], array[7]);
368 cnt = 3;
369 raw_size -= 3;
370 first = (raw_type == 3) ? 0 : 1;
371
372 state = 12;
373 goto push;
374 }
375 break;
376 case 10: break;
377 case 11:
378 vtl_type = (((uint32_t)(array[9])) << 8) | ((uint32_t)(array[10]));
379 fprintf(logfile, "{%04x}", vtl_type);
380
381 fprintf(logfile, "\t\t\t\t\t\t");
382 fprintf(logfile, "| %s: %s\n\t\t", "CMD", name_of_data(vtl_type));
383 add_data_code(vtl_type);
384
385 if (!vtl_size)
386 {
387 fprintf(logfile, "\n");
388 state = 0;
389 }
390 break;
391 default: push:
392 fprintf(logfile, "%02X ", data);
393
394 if (!(++cnt % 12))
395 {
396 fprintf(logfile, "\n\t\t");
397 }
398
399 if (--raw_size == 0)
400 {
401 fprintf(logfile, "\n");
402 state = 0;
403 }
404 break;
405 }
406
407 if (state == 0)
408 {
409 fprintf(logfile, "\n");
410 i = 0;
411 }
412 state++;
413
414 return 0;
415 }
416
417 int dusb_decomp(const char *filename)
418 {
419 char src_name[1024];
420 char dst_name[1024];
421 char line[256];
422 unsigned char data = 0;
423 unsigned int i;
424
425 snprintf(src_name, sizeof(src_name) - 1, "%s.hex", filename);
426 src_name[sizeof(src_name) - 1] = 0;
427
428 snprintf(dst_name, sizeof(dst_name) - 1, "%s.pkt", filename);
429 dst_name[sizeof(dst_name) - 1] = 0;
430
431 hex = fopen(src_name, "rt");
432 if (hex == NULL)
433 {
434 fprintf(stderr, "Unable to open input file: %s\n", src_name);
435 return -1;
436 }
437
438 logfile = fopen(dst_name, "wt");
439 if (logfile == NULL)
440 {
441 fprintf(stderr, "Unable to open output file: %s\n", dst_name);
442 fclose(hex);
443 return -1;
444 }
445
446 fprintf(logfile, "TI packet decompiler for D-USB, version 1.0\n");
447
448 // skip comments
449 if ( fgets(line, sizeof(line), hex) == NULL
450 || fgets(line, sizeof(line), hex) == NULL
451 || fgets(line, sizeof(line), hex) == NULL)
452 {
453 goto exit;
454 }
455
456 while (hex_read(&data) != -1)
457 {
458 dusb_write(0, data);
459 }
460
461 fprintf(logfile, "() Packet types found: ");
462 for (i = 0; i < ptf; i++)
463 {
464 fprintf(logfile, "%02x ", pkt_type_found[i]);
465 }
466 fprintf(logfile, "\n");
467 fprintf(logfile, "{} Data codes found: ");
468 for (i = 0; i < dcf; i++)
469 {
470 fprintf(logfile, "%04x ", data_code_found[i]);
471 }
472 fprintf(logfile, "\n");
473
474 exit:
475 fclose(logfile);
476 fclose(hex);
477
478 return 0;
479 }
480
481 #if 0
482 int main(int argc, char **argv)
483 {
484 if (argc < 2)
485 {
486 fprintf(stderr, "Usage: hex2dusb [file]\n");
487 exit(0);
488 }
489
490 return dusb_decomp(argv[1]);
491 }
492 #endif