-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patht8utils.cpp
More file actions
635 lines (597 loc) · 22.8 KB
/
t8utils.cpp
File metadata and controls
635 lines (597 loc) · 22.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/*******************************************************************************
t7utils.cpp
(c) 2011, 2012 by Sophie Dexter
portions (c) Tomi Liljemark (firstname.surname@gmail.com)
This C++ module provides functions for communicating simple messages to and from
the T7 ECU
********************************************************************************
WARNING: Use at your own risk, sadly this software comes with no guarantees.
This software is provided 'free' and in good faith, but the author does not
accept liability for any damage arising from its use.
*******************************************************************************/
#include "t8utils.h"
#include "interfaces.h"
#include "t8bootloaders.h"
Timer TesterPresent;
static const uint8_t T8BootloaderRead[] = T8_BOOTLOADER_DUMP;
static const uint8_t T8BootLoaderWrite[] = T8_BOOTLOADER_PROG;
//
// t8_initialise
//
// sends an initialisation message to the T7 ECU
// but doesn't displays anything.
//
// inputs: none
// return: bool true if there was a message, false if no message.
//
bool t8_initialise()
{
return true;
}
bool t8_show_VIN()
{
uint32_t i;
char T8TxFlo[] = T8FLOCTL;
char T8TxMsg[] = T8REQVIN;
char T8RxMsg[8];
printf("Requesting VIN from T8...\r\n");
// Send "Request VIN" to Trionic8
if (!can_send_timeout (T8TSTRID, T8TxMsg, 8, T8MESSAGETIMEOUT))
return false;
// wait for the T8 to reply
// Read "Seed"
// if a message is not received id return false
if (!can_wait_timeout(T8ECU_ID, T8RxMsg, 8, T8MESSAGETIMEOUT))
return false;
//* DEBUG info...
for (i = 0; i < 8; i++ ) printf("0x%02X ", T8RxMsg[i] );
printf("\r\n");
for (i = 5; i < 8; i++ ) printf("%c", T8RxMsg[i] );
printf("\r\n");
// Send Trionic8 a "Flow Control Message to get the rest of the VIN
if (!can_send_timeout (T8TSTRID, T8TxFlo, 8, T8MESSAGETIMEOUT))
return false;
if (!can_wait_timeout(T8ECU_ID, T8RxMsg, 8, T8MESSAGETIMEOUT))
return false;
//* DEBUG info...
for (i = 0; i < 8; i++ ) printf("0x%02X ", T8RxMsg[i] );
printf("\r\n");
for (i = 1; i < 8; i++ ) printf("%c", T8RxMsg[i] );
printf("\r\n");
if (!can_wait_timeout(T8ECU_ID, T8RxMsg, 8, T8MESSAGETIMEOUT))
return false;
//* DEBUG info...
for (i = 0; i < 8; i++ ) printf("0x%02X ", T8RxMsg[i] );
printf("\r\n");
for (i = 1; i < 8; i++ ) printf("%c", T8RxMsg[i] );
printf("\r\n");
//*/
return true;
}
bool t8_write_VIN()
{
char SetVin10[] = {0x10,0x13,0x3B,0x90,0x59,0x53,0x33,0x46};
char SetVin21[] = {0x21,0x46,0x34,0x35,0x53,0x38,0x33,0x31};
// char SetVin22[] = {0x22,0x30,0x30,0x32,0x33,0x34,0x30,0xaa}; // Original
char SetVin22[] = {0x22,0x30,0x30,0x34,0x33,0x32,0x31,0x00};
char T8RxMsg[8];
char k = 0;
// GMLANTesterPresent(T8REQID, T8RESPID);
// wait_ms(2000);
//
// printf("Requesting Security Access\r\n");
// if (!t8_authenticate(0x01)) {
// printf("Unable to get Security Access\r\n");
// return false;
// }
// printf("Security Access Granted\r\n");
//
// GMLANTesterPresent(T8REQID, T8RESPID);
// wait_ms(2000);
//
// GMLANTesterPresent(T8REQID, T8RESPID);
// wait_ms(2000);
//
if (!can_send_timeout (T8TSTRID, SetVin10, 8, T8MESSAGETIMEOUT)) {
printf("Unable to write VIN\r\n");
return false;
}
for (k = 0; k < 8; k++ ) printf("0x%02X ", SetVin10[k] );
printf("\r\n");
if (!can_wait_timeout(T8ECU_ID, T8RxMsg, 8, T8MESSAGETIMEOUT))
return false;
for (k = 0; k < 8; k++ ) printf("0x%02X ", T8RxMsg[k] );
printf("\r\n");
// wait_ms(100);
if (!can_send_timeout (T8TSTRID, SetVin21, 8, T8MESSAGETIMEOUT)) {
printf("Unable to write VIN\r\n");
return false;
}
for (k = 0; k < 8; k++ ) printf("0x%02X ", SetVin21[k] );
printf("\r\n");
// wait_ms(100);
if (!can_send_timeout (T8TSTRID, SetVin22, 8, T8MESSAGETIMEOUT)) {
printf("Unable to write VIN\r\n");
return false;
}
for (k = 0; k < 8; k++ ) printf("0x%02X ", SetVin22[k] );
printf("\r\n");
if (!can_wait_timeout(T8ECU_ID, T8RxMsg, 8, T8MESSAGETIMEOUT))
return false;
for (k = 0; k < 8; k++ ) printf("0x%02X ", T8RxMsg[k] );
printf("\r\n");
return true;
// GMLANTesterPresent(T8REQID, T8RESPID);
// wait_ms(2000);
//
}
//
// t8_authenticate
//
// sends an authentication message to the T7 ECU
// but doesn't display anything.
//
// inputs: none
// return: bool true if there was a message, false if no message.
//
bool t8_authenticate(uint32_t ReqID, uint32_t RespID, char level)
{
uint16_t i, seed, key;
// if (!GMLANSecurityAccessRequest(ReqID, RespID, level, seed)) {
// printf("Unable to request SEED value for security access\r\n");
// return false;
// }
for (i=0; i < 20; i++) {
if (GMLANSecurityAccessRequest(ReqID, RespID, level, seed))
break;
ThisThread::sleep_for(1000);
GMLANTesterPresent(ReqID, RespID);
}
if (i == 20) {
printf("Unable to request SEED value for security access\r\n");
return false;
}
if ( seed == 0x0000 ) {
printf("T8 ECU is already unlocked\r\n");
return true;
}
key = (seed >> 5) | (seed << 11);
key += 0xB988;
if (level == 0xFD) {
key /= 3;
key ^= 0x8749;
key += 0x0ACF;
key ^= 0x81BF;
} else if (level == 0xFB) {
key ^= 0x8749;
key += 0x06D3;
key ^= 0xCFDF;
}
/* CIM KEY CALCULATION
uint16_t key = (seed + 0x9130);
key = (key >> 8) | (key << 8);
key -= 0x3FC7;
*/
ThisThread::sleep_for(1);
if (!GMLANSecurityAccessSendKey(ReqID, RespID, level, key)) {
printf("Unable to send KEY value for security access\r\n");
return false;
}
printf("Key Accepted\r\n");
ThisThread::sleep_for(500); // was 5
return true;
}
//
// t8_dump
//
// dumps the T8 BIN File
// but doesn't displays anything.
//
// inputs: none
// return: bool true if there was a message, false if no message.
//
bool t8_dump()
{
uint32_t i = 0, k = 0;
char T8TxMsg[8];
char T8RxMsg[8];
timer.reset();
timer.start();
printf("Creating FLASH dump file...\r\n");
//
if (!GMLANprogrammingSetupProcess(T8REQID, T8RESPID))
return false;
//
printf("Requesting Security Access\r\n");
if (!t8_authenticate(T8REQID, T8RESPID, 0x01)) {
printf("Unable to get Security Access\r\n");
return false;
}
printf("Security Access Granted\r\n");
//
if(!GMLANprogrammingUtilityFileProcess(T8REQID, T8RESPID, T8BootloaderRead))
return false;
//
//
printf("Downloading FLASH BIN file...\r\n");
printf("Creating FLASH dump file...\r\n");
FILE *fp = fopen("/local/original.bin", "w"); // Open "original.bin" on the local file system for writing
if (!fp) {
perror ("The following error occured");
return TERM_ERR;
}
printf(" 0.00 %% complete.\r");
TesterPresent.start();
// It is possible to save some time by only reading the program code and CAL data
// This is just a rough calculation, and slight overestimate of the number of blocks of data needed to send the BIN file
T8TxMsg[0] = 0x06;
T8TxMsg[1] = 0x21;
T8TxMsg[2] = 0x80; // Blocksize
T8TxMsg[3] = 0x00; // This address (0x020140) points to the Header at the end of the BIN
T8TxMsg[4] = 0x02;
T8TxMsg[5] = 0x01;
T8TxMsg[6] = 0x40;
T8TxMsg[7] = 0xaa;
if (!can_send_timeout (T8TSTRID, T8TxMsg, 7, T8MESSAGETIMEOUT)) {
printf("Unable to download FLASH\r\n");
return false;
}
if (!can_wait_timeout(T8ECU_ID, T8RxMsg, 8, T8MESSAGETIMEOUT))
return false;
uint32_t EndAddress = (T8RxMsg[5] << 16) | (T8RxMsg[6] << 8) | T8RxMsg[7];
EndAddress += 0x200; // Add some bytes for the Footer itself and to account for division rounded down later
char T8TxFlo[] = T8FLOCTL;
can_send_timeout (T8TSTRID, T8TxFlo, 8, T8MESSAGETIMEOUT);
for (i = 0; i < 0x12; i++) {
if (!can_wait_timeout(T8ECU_ID, T8RxMsg, 8, T8MESSAGETIMEOUT))
return false;
}
printf("Reading your BIN file adjusted for footer = 0x%06lX Bytes\r\n", EndAddress );
for ( uint32_t StartAddress = 0x0; StartAddress < EndAddress; StartAddress +=0x80 ) { // 0x100000
T8TxMsg[0] = 0x06;
T8TxMsg[1] = 0x21;
T8TxMsg[2] = 0x80; // Blocksize
T8TxMsg[3] = (char) (StartAddress >> 24);
T8TxMsg[4] = (char) (StartAddress >> 16);
T8TxMsg[5] = (char) (StartAddress >> 8);
T8TxMsg[6] = (char) (StartAddress);
T8TxMsg[7] = 0xaa;
#ifdef DEBUG
printf("block %#.3f\r\n",timer.read());
#endif
if (!can_send_timeout (T8TSTRID, T8TxMsg, 7, T8MESSAGETIMEOUT)) {
printf("Unable to download FLASH\r\n");
return false;
}
if (!can_wait_timeout(T8ECU_ID, T8RxMsg, 8, T8MESSAGETIMEOUT))
return false;
#ifdef DEBUG
printf("first %#.3f\r\n",timer.read());
#endif
uint32_t txpnt = 0;
for (k = 4; k < 8; k++ ) file_buffer[txpnt++] = T8RxMsg[k];
uint8_t DataFrames = 0x12;
char iFrameNumber = 0x21;
char T8TxFlo[] = T8FLOCTL;
can_send_timeout (T8TSTRID, T8TxFlo, 8, T8MESSAGETIMEOUT);
#ifdef DEBUG
printf("flowCtrl %#.3f\r\n",timer.read());
#endif
for (i = 0; i < DataFrames; i++) {
if (!can_wait_timeout(T8ECU_ID, T8RxMsg, 8, T8MESSAGETIMEOUT))
return false;
#ifdef DEBUG
printf("Consec %#.3f\r\n",timer.read());
#endif
iFrameNumber++;
for (k = 1; k < 8; k++ ) file_buffer[txpnt++] = T8RxMsg[k];
}
fwrite((file_buffer), 1, 0x80, fp);
if (ferror (fp)) {
fclose (fp);
printf ("Error writing to the FLASH BIN file.\r\n");
return TERM_ERR;
}
printf("%6.2f\r", (100.0*(float)StartAddress)/(float)(EndAddress) );
if (TesterPresent.read_ms() > 2000) {
GMLANTesterPresent(T8REQID, T8RESPID);
TesterPresent.reset();
}
}
for (uint32_t i = 0; i < 0x80; i++)
file_buffer[i] = 0xFF;
while ( ftell(fp) < 0x100000 ) {
// for ( uint32_t StartAddress = EndAddress; StartAddress < 0x100000; StartAddress +=0x80 ) {
fwrite((file_buffer), 1, 0x80, fp);
if (ferror (fp)) {
fclose (fp);
printf ("Error writing to the FLASH BIN file.\r\n");
return TERM_ERR;
}
}
printf("%6.2f\r\n", (float)100 );
timer.stop();
printf("SUCCESS! Getting the FLASH dump took %#.1f seconds.\r\n",timer.read());
fclose(fp);
return true;
}
bool t8_flash()
{
uint32_t i = 0, j = 0, k = 0;
timer.reset();
timer.start();
printf("FLASHing T8 BIN file...\r\n");
//
if (!GMLANprogrammingSetupProcess(T8REQID, T8RESPID))
return false;
//
printf("Requesting Security Access\r\n");
if (!t8_authenticate(T8REQID, T8RESPID, 0x01)) {
printf("Unable to get Security Access\r\n");
return false;
}
printf("Security Access Granted\r\n");
//
// All steps needed to transfer and start a bootloader ('Utility File' in GMLAN parlance)
// const uint8_t BootLoader[] = T8BootloaderProg;
// if(!GMLANprogrammingUtilityFileProcess(T8REQID, T8RESPID, BootLoader))
if(!GMLANprogrammingUtilityFileProcess(T8REQID, T8RESPID, T8BootLoaderWrite))
return false;
//
uint32_t StartAddress = 0x020000;
uint32_t txpnt = 0;
char iFrameNumber = 0x21;
char GMLANMsg[8];
char data2Send[0xE0];
//
// fopen modified.bin here, check it is OK and work out how much data I need to send
// need lots of fcloses though
printf("Checking the FLASH BIN file...\r\n");
FILE *fp = fopen("/local/modified.bin", "r"); // Open "modified.bin" on the local file system for reading
if (!fp) {
printf("Error: I could not find the BIN file MODIFIED.BIN\r\n");;
return TERM_ERR;
}
// obtain file size - it should match the size of the FLASH chips:
fseek (fp , 0 , SEEK_END);
uint32_t file_size = ftell (fp);
rewind (fp);
// read the initial stack pointer value in the BIN file - it should match the value expected for the type of ECU
uint32_t stack_long = 0;
if (!fread(&stack_long,4,1,fp)) {
fclose(fp);
return TERM_ERR;
}
stack_long = (stack_long >> 24) | ((stack_long << 8) & 0x00FF0000) | ((stack_long >> 8) & 0x0000FF00) | (stack_long << 24);
//
if (file_size != T8FLASHSIZE || stack_long != T8POINTER) {
fclose(fp);
printf("The BIN file does not appear to be for a T8 ECU :-(\r\n");
printf("BIN file size: %#010lx, FLASH chip size: %#010x, Pointer: %#010lx.\r\n", file_size, T7FLASHSIZE, stack_long);
return TERM_ERR;
}
// It is possible to save some time by only sending the program code and CAL data
// This is just a rough calculation, and slight overestimate of the number of blocks of data needed to send the BIN file
uint32_t blocks2Send;
fseek(fp,0x020140,SEEK_SET);
if (!fread(&blocks2Send,4,1,fp)) {
fclose(fp);
return TERM_ERR;
}
blocks2Send = (blocks2Send >> 24) | ((blocks2Send << 8) & 0x00FF0000) | ((blocks2Send >> 8) & 0x0000FF00) | (blocks2Send << 24);
printf("Start address of BIN file's Footer area = 0x%06lX\r\n", blocks2Send );
blocks2Send += 0x200; // Add some bytes for the Footer itself and to account for division rounded down later
blocks2Send -= 0x020000; // Remove 0x020000 because we don't send the bootblock and adaptation blocks
printf("Amount of data to send BIN file adjusted for footer = 0x%06lX Bytes\r\n", blocks2Send );
blocks2Send /= 0xE0;
printf("Number of Blocks of 0xE0 Bytes needed to send BIN file = 0x%04lX\r\n", blocks2Send );
// Move BIN file pointer to start of data
fseek (fp , 0x020000 , SEEK_SET);
// Erase the FLASH
printf("Waiting for FLASH to be Erased\r\n");
if (!GMLANRequestDownload(T8REQID, T8RESPID, GMLANRequestDownloadModeEncrypted)) {
fclose(fp);
printf("Unable to erase the FLASH chip!\r\n");
return false;
}
// Now send the BIN file
GMLANTesterPresent(T8REQID, T8RESPID);
TesterPresent.start();
printf("Sending FLASH BIN file\r\n");
printf(" 0.00 %% complete.\r");
for (i=0; i<blocks2Send; i++) {
// get a block of 0xE0 bytes in an array called data2Send
if (!fread(data2Send,0xE0,1,fp)) {
fclose(fp);
printf("\r\nError reading the BIN file MODIFIED.BIN\r\n");
return false;
}
// encrypt data2Send array by XORing with 6 different values in a ring (modulo function)
char key[6] = { 0x39, 0x68, 0x77, 0x6D, 0x47, 0x39 };
for ( j = 0; j < 0xE0; j++ )
data2Send[j] ^= key[(((0xE0*i)+j) % 6)];
// Send the block of data
if (!GMLANDataTransferFirstFrame(T8REQID, T8RESPID, 0xE6, GMLANDOWNLOAD, StartAddress)) {
fclose(fp);
printf("\r\nUnable to start BIN File Upload\r\n");
return false;
}
// Send 0x20 messages of 0x07 bytes each (0x20 * 0x07 = 0xE0)
txpnt = 0;
iFrameNumber = 0x21;
for (j=0; j < 0x20; j++) {
GMLANMsg[0] = iFrameNumber;
for (k=1; k<8; k++)
GMLANMsg[k] = data2Send[txpnt++];
if (!can_send_timeout(T8REQID, GMLANMsg, 8, GMLANPTCT)) {
fclose(fp);
printf("\r\nUnable to send BIN File\r\n");
return false;
}
++iFrameNumber &= 0x2F;
wait_us(1000); // can be as low as 250 for an ECU on its own, but need 1000 (1ms) to work in a car (use a longer delay to be ultrasafe??? )
}
if (!GMLANDataTransferBlockAcknowledge(T8RESPID)) {
fclose(fp);
return false;
}
if (TesterPresent.read_ms() > 2000) {
GMLANTesterPresent(T8REQID, T8RESPID);
TesterPresent.reset();
}
StartAddress += 0xE0;
printf("%6.2f\r", (100.0*(float)i)/(float)(blocks2Send) );
}
// FLASHing complete
printf("%6.2f\r\n", (float)100 );
// End programming session and return to normal mode
if (!GMLANReturnToNormalMode(T8REQID, T8RESPID)) {
fclose(fp);
printf("UH-OH! T8 ECU did not Return To Normal Mode!!\r\n");
return false;
}
timer.stop();
printf("SUCCESS! FLASHing the BIN file took %#.1f seconds.\r\n",timer.read());
fclose(fp);
return true;
}
bool t8_recover()
{
uint32_t i = 0, j = 0, k = 0;
timer.reset();
timer.start();
printf("Recovering your T8 ECU ...\r\n");
//
if (!GMLANprogrammingSetupProcess(T8USDTREQID, T8UUDTRESPID))
return false;
//
printf("Requesting Security Access\r\n");
if (!t8_authenticate(T8USDTREQID, T8UUDTRESPID, 0x01)) {
printf("Unable to get Security Access\r\n");
return false;
}
printf("Security Access Granted\r\n");
//
// const uint8_t BootLoader[] = T8BootloaderProg;
// if(!GMLANprogrammingUtilityFileProcess(T8USDTREQID, T8UUDTRESPID, BootLoader))
if(!GMLANprogrammingUtilityFileProcess(T8USDTREQID, T8UUDTRESPID, T8BootLoaderWrite))
return false;
//
// All steps needed to transfer and start a bootloader ('Utility File' in GMLAN parlance)
uint32_t StartAddress = 0x020000;
uint32_t txpnt = 0;
char iFrameNumber = 0x21;
char GMLANMsg[8];
char data2Send[0xE0];
//
// fopen modified.bin here, check it is OK and work out how much data I need to send
// need lots of fcloses though
printf("Checking the FLASH BIN file...\r\n");
FILE *fp = fopen("/local/modified.bin", "r"); // Open "modified.bin" on the local file system for reading
if (!fp) {
printf("Error: I could not find the BIN file MODIFIED.BIN\r\n");;
return TERM_ERR;
}
// obtain file size - it should match the size of the FLASH chips:
fseek (fp , 0 , SEEK_END);
uint32_t file_size = ftell (fp);
rewind (fp);
// read the initial stack pointer value in the BIN file - it should match the value expected for the type of ECU
uint32_t stack_long = 0;
if (!fread(&stack_long,4,1,fp)) {
fclose(fp);
return TERM_ERR;
}
stack_long = (stack_long >> 24) | ((stack_long << 8) & 0x00FF0000) | ((stack_long >> 8) & 0x0000FF00) | (stack_long << 24);
//
if (file_size != T8FLASHSIZE || stack_long != T8POINTER) {
fclose(fp);
printf("The BIN file does not appear to be for a T8 ECU :-(\r\n");
printf("BIN file size: %#010lx, FLASH chip size: %#010x, Pointer: %#010lx.\r\n", file_size, T7FLASHSIZE, stack_long);
return TERM_ERR;
}
// It is possible to save some time by only sending the program code and CAL data
// This is just a rough calculation, and slight overestimate of the number of blocks of data needed to send the BIN file
uint32_t blocks2Send;
fseek(fp,0x020140,SEEK_SET);
if (!fread(&blocks2Send,4,1,fp)) {
fclose(fp);
return TERM_ERR;
}
blocks2Send = (blocks2Send >> 24) | ((blocks2Send << 8) & 0x00FF0000) | ((blocks2Send >> 8) & 0x0000FF00) | (blocks2Send << 24);
printf("Start address of BIN file's Footer area = 0x%06lX\r\n", blocks2Send );
blocks2Send += 0x200; // Add some bytes for the Footer itself and to account for division rounded down later
blocks2Send -= 0x020000; // Remove 0x020000 because we don't send the bootblock and adaptation blocks
printf("Amount of data to send BIN file adjusted for footer = 0x%06lX Bytes\r\n", blocks2Send );
blocks2Send /= 0xE0;
printf("Number of Blocks of 0xE0 Bytes needed to send BIN file = 0x%04lX\r\n", blocks2Send );
// Move BIN file pointer to start of data
fseek (fp , 0x020000 , SEEK_SET);
// Erase the FLASH
printf("Waiting for FLASH to be Erased\r\n");
if (!GMLANRequestDownload(T8REQID, T8RESPID, GMLANRequestDownloadModeEncrypted)) {
fclose(fp);
printf("Unable to erase the FLASH chip!\r\n");
return false;
}
// Now send the BIN file
GMLANTesterPresent(T8REQID, T8RESPID);
TesterPresent.start();
printf("Sending FLASH BIN file\r\n");
printf(" 0.00 %% complete.\r");
for (i=0; i<blocks2Send; i++) {
// get a block of 0xE0 bytes in an array called data2Send
if (!fread(data2Send,0xE0,1,fp)) {
fclose(fp);
printf("\r\nError reading the BIN file MODIFIED.BIN\r\n");
return false;
}
// encrypt data2Send array by XORing with 6 different values in a ring (modulo function)
char key[6] = { 0x39, 0x68, 0x77, 0x6D, 0x47, 0x39 };
for ( j = 0; j < 0xE0; j++ )
data2Send[j] ^= key[(((0xE0*i)+j) % 6)];
// Send the block of data
if (!GMLANDataTransferFirstFrame(T8REQID, T8RESPID, 0xE6, GMLANDOWNLOAD, StartAddress)) {
fclose(fp);
printf("\r\nUnable to start BIN File Upload\r\n");
return false;
}
// Send 0x20 messages of 0x07 bytes each (0x20 * 0x07 = 0xE0)
txpnt = 0;
iFrameNumber = 0x21;
for (j=0; j < 0x20; j++) {
GMLANMsg[0] = iFrameNumber;
for (k=1; k<8; k++)
GMLANMsg[k] = data2Send[txpnt++];
if (!can_send_timeout(T8REQID, GMLANMsg, 8, GMLANPTCT)) {
fclose(fp);
printf("\r\nUnable to send BIN File\r\n");
return false;
}
++iFrameNumber &= 0x2F;
wait_us(1000); // was 250 use 1000 or wait_ms(1) to be ultrasafe
}
if (!GMLANDataTransferBlockAcknowledge(T8RESPID)) {
fclose(fp);
return false;
}
if (TesterPresent.read_ms() > 2000) {
GMLANTesterPresent(T8REQID, T8RESPID);
TesterPresent.reset();
}
StartAddress += 0xE0;
printf("%6.2f\r", (100.0*(float)i)/(float)(blocks2Send) );
}
// FLASHing complete
printf("%6.2f\r\n", (float)100 );
// End programming session and return to normal mode
if (!GMLANReturnToNormalMode(T8REQID, T8RESPID)) {
fclose(fp);
printf("UH-OH! T8 ECU did not Return To Normal Mode!!\r\n");
return false;
}
timer.stop();
fclose(fp);
printf("SUCCESS: Your T8 ECU has been recovered.\r\n");
return true;
}