Debian Codegraph @

src/hex2nsp.cc

raw
1 /* hex2nsp - 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 <conio.h>
20 #include <ctype.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25
26 #include "ticables.h"
27 #include "logging.h"
28 #include "internal.h"
29
30 #define HEXDUMP_SIZE 12
31
32 #define ARRAY_SIZE (256)
33
34 /*
35 Format (see http://hackspire.unsads.com/USB_Protocol#Service_identifiers):
36
37 54 FD SA SA SS SS DA DA DS DS DC DC SZ AK SQ CK [data part]
38 */
39
40 typedef struct
41 {
42 uint16_t unused;
43 uint16_t src_addr;
44 uint16_t src_id;
45 uint16_t dst_addr;
46 uint16_t dst_id;
47 uint16_t data_sum;
48 uint8_t data_size;
49 uint8_t ack;
50 uint8_t seq;
51 uint8_t hdr_sum;
52 } Packet;
53
54 typedef struct
55 {
56 uint16_t value;
57 const char* name;
58 } ServiceId;
59
60 typedef struct
61 {
62 uint16_t value;
63 const char* name;
64 } Address;
65
66 static const Address addrs[] =
67 {
68 { 0x0000, "TI" },
69 { 0x6400, "PC" },
70 { 0x6401, "TI" },
71 { 0, NULL },
72 };
73
74 static const ServiceId sids[] =
75 {
76 { 0x00FE, "Reception Acknowledgment" },
77 { 0x00FF, "Reception Ack" },
78 { 0x4001, "Null" },
79 { 0x4002, "Echo" },
80 { 0x4003, "Device Address Request/Assignment" },
81 { 0x4020, "Device Information" },
82 { 0x4021, "Screen Capture" },
83 { 0x4022, "Event" },
84 { 0x4023, "Shutdown" },
85 { 0x4024, "Screen Capture w/ RLE" },
86 { 0x4041, "Activity" },
87 { 0x4042, "Keypresses" },
88 { 0x4043, "RPC" },
89 { 0x4050, "Login" },
90 { 0x4051, "Messages" },
91 { 0x4054, "Hub Connection" },
92 { 0x4060, "File Management" },
93 { 0x4080, "OS Installation" },
94 { 0x4090, "Remote Handheld Management" },
95 { 0x40DE, "Service Disconnect" },
96 { 0x5000, "Ext Echo" },
97
98 { 0x8003, "8003" },
99 { 0x8004, "8004" },
100 { 0x8005, "8005" },
101 { 0x8006, "8006" },
102 { 0x8007, "8007" },
103 { 0x8009, "8009" },
104
105 { 0, NULL },
106 };
107
108 /* */
109
110 /*static int is_a_sid(uint8_t id)
111 {
112 int i;
113
114 for(i=0; sids[i].name; i++)
115 if(id == sids[i].value)
116 break;
117 return i;
118 }*/
119
120 static const char* name_of_sid(uint16_t id)
121 {
122 int i;
123
124 for (i=0; sids[i].name; i++)
125 {
126 if (id == sids[i].value)
127 {
128 return sids[i].name;
129 }
130 }
131
132 return "???";
133 }
134
135 static const char* name_of_addr(uint16_t addr)
136 {
137 int i;
138
139 for (i=0; addrs[i].name; i++)
140 {
141 if (addr == addrs[i].value)
142 {
143 return addrs[i].name;
144 }
145 }
146
147 return "??";
148 }
149
150 /*static const char* ep_way(int ep)
151 {
152 if(ep == 0x01) return "TI>PC";
153 else if(ep == 0x02) return "PC>TI";
154 else return "XX>XX";
155 }*/
156
157 /* */
158
159 static int add_sid(uint16_t* array, uint16_t id, int *count)
160 {
161 int i;
162 for (i = 0; i < *count; i++)
163 {
164 if (array[i] == id)
165 {
166 return 0;
167 }
168 }
169
170 if (i < ARRAY_SIZE - 1)
171 {
172 array[++i] = id;
173 }
174 else
175 {
176 static int warn_add_sid;
177 if (!warn_add_sid++)
178 {
179 ticables_warning("NSP protocol interpreter: no room left in sid_found array.");
180 }
181 }
182
183 *count = i;
184
185 return i;
186 }
187
188 static int add_addr(uint16_t* array, uint16_t addr, int *count)
189 {
190 int i;
191
192 for (i = 0; i < *count; i++)
193 {
194 if (array[i] == addr)
195 {
196 return 0;
197 }
198 }
199
200 if (i < ARRAY_SIZE - 1)
201 {
202 array[++i] = addr;
203 }
204 else
205 {
206 static int warn_add_addr;
207 if (!warn_add_addr++)
208 {
209 ticables_warning("NSP protocol interpreter: no room left in addr_found array.");
210 }
211 }
212
213 *count = i;
214
215 return i;
216 }
217
218 /* */
219
220 static FILE *hex = NULL;
221 static FILE *logfile = NULL;
222
223 static int hex_read(unsigned char *data)
224 {
225 static int idx = 0;
226 int ret;
227 int data2;
228
229 if (feof(hex))
230 {
231 return -1;
232 }
233
234 ret = fscanf(hex, "%02X", &data2);
235 if (ret < 1)
236 {
237 return -1;
238 }
239 *data = data2 & 0xFF;
240 if (fgetc(hex) < 0)
241 {
242 return -1;
243 }
244 idx++;
245
246 if (idx >= 16)
247 {
248 int i;
249
250 idx = 0;
251 for (i = 0; (i < 67-49) && !feof(hex); i++)
252 {
253 if (fgetc(hex) < 0)
254 {
255 return -1;
256 }
257 }
258 }
259
260 return 0;
261 }
262
263 static uint16_t sid_found[ARRAY_SIZE] = { 0 };
264 static uint16_t addr_found[ARRAY_SIZE] = { 0 };
265 static int sif=0, af=0;
266
267 static int dusb_write(int dir, uint8_t data)
268 {
269 static uint8_t array[20];
270 static int i = 0;
271 static unsigned long state = 1;
272 static uint16_t src_addr, src_id;
273 static uint16_t dst_addr, dst_id;
274 static uint8_t data_size, ack, sq;
275 static int cnt;
276 static uint8_t ascii[HEXDUMP_SIZE+1];
277
278 if (logfile == NULL)
279 {
280 return -1;
281 }
282
283 array[i++ % 16] = data;
284
285 switch(state) // Finite State Machine
286 {
287 case 1: // unused
288 case 2:
289 break;
290
291 case 3: // source address
292 break;
293 case 4:
294 src_addr = (((uint16_t)(array[2])) << 8) | ((uint16_t)(array[3]));
295 fprintf(logfile, "%04x:", src_addr);
296 add_addr(addr_found, src_addr, &af);
297 break;
298
299 case 5: // source service id
300 break;
301 case 6:
302 src_id = (((uint16_t)(array[4])) << 8) | ((uint16_t)(array[5]));
303 fprintf(logfile, "%04x->", src_id);
304 add_sid(sid_found, src_id, &sif);
305 break;
306
307 case 7: // destination address
308 break;
309 case 8:
310 dst_addr = (((uint16_t)(array[6])) << 8) | ((uint16_t)(array[7]));
311 fprintf(logfile, "%04x:", dst_addr);
312 add_addr(addr_found, dst_addr, &af);
313 break;
314
315 case 9: // destination service id
316 break;
317 case 10:
318 dst_id = (((uint16_t)(array[8])) << 8) | ((uint16_t)(array[9]));
319 fprintf(logfile, "%04x ", dst_id);
320 add_sid(sid_found, src_id, &sif);
321 break;
322
323 case 11: break; // data checksum
324 case 12: break;
325
326 case 13: // data size
327 data_size = array[12];
328 break;
329
330 case 14: // acknowledgment
331 ack = array[13];
332 fprintf(logfile, "AK=%02x ", ack);
333 break;
334
335 case 15: // sequence number
336 sq = array[14];
337 fprintf(logfile, "SQ=%02x ", sq);
338 break;
339
340 case 16: // header checksum
341 fprintf(logfile, "(%3i bytes) ", data_size);
342 cnt = 0;
343
344 fprintf(logfile, "\t\t\t\t\t%s (%s) ==> %s (%s)\n",
345 name_of_addr(src_addr), name_of_sid(src_id),
346 name_of_addr(dst_addr), name_of_sid(dst_id));
347
348 if (data_size == 0)
349 {
350 state = 0;
351 }
352 break;
353
354 default:
355 if (!cnt)
356 {
357 fprintf(logfile, "\t\t");
358 }
359
360 fprintf(logfile, "%02X ", data);
361 ascii[cnt % HEXDUMP_SIZE] = data;
362
363 if (!(++cnt % HEXDUMP_SIZE))
364 {
365 fprintf(logfile, " | ");
366 for (i = 0; i < HEXDUMP_SIZE; i++)
367 {
368 fprintf(logfile, "%c", isalnum(ascii[i]) ? ascii[i] : '.');
369 }
370
371 fprintf(logfile, "\n\t\t");
372 }
373
374 if (--data_size == 0)
375 {
376 for (i = 0; i < HEXDUMP_SIZE - (cnt%HEXDUMP_SIZE); i++)
377 {
378 fprintf(logfile, " ");
379 }
380 fprintf(logfile, " | ");
381 for (i = 0; i < (cnt%HEXDUMP_SIZE); i++)
382 {
383 fprintf(logfile, "%c", isalnum(ascii[i]) ? ascii[i] : '.');
384 }
385
386 fprintf(logfile, "\n");
387 state = 0;
388 }
389 break;
390 }
391
392 if(state == 0)
393 {
394 fprintf(logfile, "\n");
395 i = 0;
396 }
397 state++;
398
399 return 0;
400 }
401
402 int nsp_decomp(const char *filename)
403 {
404 char src_name[1024];
405 char dst_name[1024];
406 char line[256];
407 unsigned char data = 0;
408 int i;
409
410 snprintf(src_name, sizeof(src_name) - 1, "%s.hex", filename);
411 src_name[sizeof(src_name) - 1] = 0;
412
413 snprintf(dst_name, sizeof(dst_name) - 1, "%s.pkt", filename);
414 dst_name[sizeof(dst_name) - 1] = 0;
415
416 hex = fopen(src_name, "rt");
417 if (hex == NULL)
418 {
419 fprintf(stderr, "Unable to open this file: %s\n", src_name);
420 return -1;
421 }
422
423 logfile = fopen(dst_name, "wt");
424 if (logfile == NULL)
425 {
426 fprintf(stderr, "Unable to open this file: %s\n", dst_name);
427 fclose(hex);
428 return -1;
429 }
430
431 fprintf(logfile, "TI packet decompiler for NSpire, version 1.0\n");
432
433 // skip comments
434 if ( fgets(line, sizeof(line), hex) == NULL
435 || fgets(line, sizeof(line), hex) == NULL
436 || fgets(line, sizeof(line), hex) == NULL)
437 {
438 goto exit;
439 }
440
441 while (hex_read(&data) != -1)
442 {
443 dusb_write(0, data);
444 }
445
446 fprintf(logfile, "() Service IDs found: ");
447 for(i = 0; i < sif; i++)
448 {
449 fprintf(logfile, "%04x ", sid_found[i]);
450 }
451 fprintf(logfile, "\n");
452 fprintf(logfile, "() Addresses found: ");
453 for(i = 0; i < af; i++)
454 {
455 fprintf(logfile, "%04x ", addr_found[i]);
456 }
457 fprintf(logfile, "\n");
458
459 exit:
460 fclose(logfile);
461 fclose(hex);
462
463 return 0;
464 }
465
466 #if 0
467 int main(int argc, char **argv)
468 {
469 if(argc < 2)
470 {
471 fprintf(stderr, "Usage: hex2nsp [file]\n");
472 exit(0);
473 }
474
475 return nsp_decomp(argv[1]);
476 }
477 #endif