-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
np_cli.c
2313 lines (1915 loc) · 56.9 KB
/
np_cli.c
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* nisprog - Nissan ECU communications utility
*
* (c) 2014-2016 fenugrec
* Licensed under GPLv3
*
* the CLI commands are implemented in here.
*/
#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stypes.h"
#include "diag.h"
#include "diag_l0.h"
#include "diag_l1.h"
#include "diag_l2.h"
#include "diag_os.h"
#include "diag_tty.h" //for setspeed
#include "diag_l2_iso14230.h" //needed to force header type (nisprog)
#include "scantool_cli.h"
#include "nisprog.h"
#include "nis_backend.h"
#include "npk_backend.h"
#include "ssm_backend.h"
#include "nissutils/cli_utils/nislib.h"
#include "nissutils/cli_utils/ecuid_list.h"
#include "npkern/iso_cmds.h"
#define CURFILE "np_cli.c" //XXXXX TODO: fix VS automagic macro setting
#define NPK_SPEED 62500 //bps default speed for npkern kernel
typedef long nparam_val; //type of .val member
/** simpler parameter unit than diag_cfgi */
struct nparam_t {
long val;
const char *shortname;
const char *descr;
long min;
long max; //validation : (val >= min) && (val <= max)
};
static struct nparam_t nparam_p3 = {.val = 5, .shortname = "p3", .descr = "P3 time before new request (ms)",
.min = 0, .max = 500};
static struct nparam_t nparam_rxe = {.val = 20, .shortname = "rxe", .descr = "Read timeout offset. Adjust to eliminate timeout errors",
.min = -20, .max = 500};
static struct nparam_t nparam_eepr = {.val = 0, .shortname = "eepr", .descr = "eeprom_read() function address",
.min = 0, .max = 2048L * 1024};
static struct nparam_t nparam_kspeed = {.val = NPK_SPEED, .shortname = "kspeed", .descr = "kernel comms speed used by \"initk\" command",
.min = 100, .max = 65000};
static struct nparam_t *nparams[] = {
&nparam_p3,
&nparam_rxe,
&nparam_eepr,
&nparam_kspeed,
NULL
};
struct nisecu_t nisecu;
/** some static data for in here only */
static struct keyset_t customkey;
/** fwd decls **/
static int npkern_init(void);
static int npk_dump(FILE *fpl, uint32_t start, uint32_t len, bool eep);
static int dump_fast(FILE *outf, const uint32_t start, uint32_t len);
static uint32_t read_ac(uint8_t *dest, uint32_t addr, uint32_t len);
static int npk_RMBA(uint8_t *dest, uint32_t addr, uint32_t len);
static bool set_keyset(u32 s27k);
/* called every time a parameter is changed from the UI.
* For some params, the value is only used in certain places,
* and therefore doesn't need to be handled in here
*/
static void update_params(void) {
if (global_l2_conn) {
global_l2_conn->diag_l2_p3min = (u16) nparam_p3.val;
global_l2_conn->diag_l2_p4min=0;
}
return;
}
/* npconf <paramname> <value>
*/
enum cli_retval cmd_npconf(int argc, char **argv) {
struct nparam_t *npt;
nparam_val tempval;
bool found = 0;
bool helping = 0;
if ((argc <= 1) || (argc > 3)) {
return CMD_USAGE;
}
if (argv[1][0] == '?') {
helping = 1;
printf("param\tvalue\tdescription\n");
}
// find param name in list
unsigned i;
for (i = 0; nparams[i]; i++) {
npt = nparams[i];
if (helping) {
printf("%s\t%ld\t%s\n", npt->shortname, npt->val, npt->descr);
continue;
}
if (strcmp(npt->shortname, argv[1]) == 0) {
found = 1;
break;
}
}
if (helping) {
return CMD_USAGE;
}
if (!found) {
printf("Unknown param \"%s\"\n", argv[1]);
return CMD_FAILED;
}
if (argc == 2) {
//no new value : just print current setting
printf("%s is currently %ld (0x%lX)\n", npt->shortname,
npt->val, npt->val);
return CMD_OK;
}
tempval = htoi(argv[2]);
if ((tempval < npt->min) || (tempval > npt->max)) {
printf("Error, requested value (%ld / 0x%lX) out of bounds !\n",
(long) tempval, (long) tempval);
return CMD_FAILED;
}
npt->val = tempval;
update_params();
printf("\t%s set to %ld (0x%lX).\n", argv[1], npt->val, npt->val);
return CMD_OK;
}
/* "dumpmem <file> <start> <len> [eep]" */
enum cli_retval cmd_dumpmem(int argc, char **argv) {
u32 start, len;
FILE *fpl;
bool eep = 0;
if (npstate == NP_DISC) {
printf("Not connected !\n");
return CMD_FAILED;
}
if ((argc < 4) || (argc > 5)) {
return CMD_USAGE;
}
if (argc == 5) {
if (strcmp("eep", argv[4]) == 0) {
eep = 1;
if (npstate != NP_NPKCONN) {
printf("Kernel must be running for reading EEPROM. Try \"runkernel\" or \"initk\"\n");
return CMD_FAILED;
}
if (!nparam_eepr.val) {
printf("Must set eeprom read function address first ! See \"npconf ?\"\n");
return CMD_FAILED;
}
if (set_eepr_addr((u32) nparam_eepr.val)) {
printf("could not set eep_read() address!\n");
return CMD_FAILED;
}
} else {
printf("did not recognize \"%s\"\n", argv[4]);
return CMD_FAILED;
}
}
//TODO : check for overwrite / append ?
if ((fpl = fopen(argv[1], "wb"))==NULL) {
printf("Cannot open %s !\n", argv[1]);
return CMD_FAILED;
}
start = (uint32_t) htoi(argv[2]);
len = (uint32_t) htoi(argv[3]);
if ((start == len) && (start == 0)) {
//special mode : dump all ROM as specified by device type.
const struct flashdev_t *fdt = nisecu.flashdev;
if (!fdt) {
printf("device type not set. Try setdev, or specify bounds manually.\n");
fclose(fpl);
return CMD_FAILED;
}
len = fdt->romsize;
}
/* Dispatch according to current state */
if (npstate == NP_NPKCONN) {
if (npk_dump(fpl, start, len, eep)) {
fclose(fpl);
return CMD_FAILED;
}
fclose(fpl);
return CMD_OK;
}
// npstate == NP_NORMALCONN:
if (dump_fast(fpl, start, len)) {
fclose(fpl);
return CMD_FAILED;
}
fclose(fpl);
return CMD_OK;
}
#define KEY_CANDIDATES 3
#define KEY_MAXDIST 10 //do not use keys that are way off
void autoselect_keyset(void) {
unsigned i;
u32 s27k;
struct ecuid_keymatch_t kcs[KEY_CANDIDATES];
ecuid_getkeys((const char *) nisecu.ecuid, kcs, KEY_CANDIDATES);
printf("Key candidate\tdist (smaller is better)\n");
for (i = 0; i < KEY_CANDIDATES; i++) {
printf("%u: 0x%08lX\t%d\n",i, (unsigned long) kcs[i].key, kcs[i].dist);
}
printf("\n");
// TODO : allow user selection
if (kcs[0].dist > KEY_MAXDIST) {
printf("ECUID matching was not conclusive, keyset unknown. Try gk or setkeys\n");
return;
}
s27k = kcs[0].key;
(void) set_keyset(s27k);
printf("Using best choice, SID27 key=%08lX. Use \"setkeys\" to change if required.\n",
(unsigned long) s27k);
return;
}
/* setdev <device_#> */
enum cli_retval cmd_setdev(int argc, char **argv) {
bool helping = 0;
unsigned idx;
const struct flashdev_t *fdt = nisecu.flashdev;
if (argc != 2) {
if (fdt) {
printf("current setting : %s\n", fdt->name);
}
return CMD_USAGE;
}
if (argv[1][0] == '?') {
helping = 1;
printf("\tname\tROM size\n");
}
for (idx=0; flashdevices[idx].name; idx++) {
if (helping) {
printf("\t%s\t%uk\n", flashdevices[idx].name, (unsigned) flashdevices[idx].romsize / 1024);
continue;
}
if (strcmp(flashdevices[idx].name, argv[1]) == 0) {
nisecu.flashdev = &flashdevices[idx];
printf("now using %s.\n", flashdevices[idx].name);
return CMD_OK;
}
}
if (helping) {
return CMD_USAGE;
}
printf("Invalid device, see list with \"setdev ?\"\n");
return CMD_FAILED;
}
/* setkeys <sid27key> [<sid36key1>] */
enum cli_retval cmd_setkeys(int argc, char **argv) {
const struct keyset_t *pks;
if (argc == 2) {
if (argv[1][0] == '?') {
return CMD_USAGE;
}
}
if ((argc < 2) || (argc > 3)) {
return CMD_USAGE;
}
u32 s27k = (u32) htoi(argv[1]);
customkey.s27k = s27k;
if (argc == 3) {
// use specified keys : don't lookup in known_keys[]
customkey.s36k1 = (u32) htoi(argv[2]);
nisecu.keyset = &customkey;
goto goodexit;
}
if (set_keyset(s27k)) {
goto goodexit;
}
printf("Does not match a known keyset; you will need to provide both s27 and s36 keys\n");
return CMD_FAILED;
goodexit:
pks = nisecu.keyset;
printf("Now using SID27 key=%08lX, SID36 key1=%08lX\n",
(unsigned long) pks->s27k, (unsigned long) pks->s36k1);
return CMD_OK;
}
enum cli_retval cmd_initk(int argc, char **argv) {
const char *npk_id;
(void) argv;
if (argc > 1) {
return CMD_USAGE;
}
if ((npstate == NP_DISC) ||
(global_state == STATE_IDLE)) {
printf("Error : not connected\n");
return CMD_FAILED;
}
if (npkern_init()) {
printf("npkern_init() error\n");
return CMD_FAILED;
}
npstate = NP_NPKCONN;
npk_id = get_npk_id();
if (npk_id) {
printf("Connected to kernel: %s\n", npk_id);
}
return CMD_OK;
}
enum cli_retval cmd_npconn(int argc, char **argv) {
(void) argv;
if (argc > 1) {
return CMD_USAGE;
}
if ((npstate != NP_DISC) ||
(global_state != STATE_IDLE)) {
printf("Error : already connected\n");
return CMD_FAILED;
}
nisecu_cleardata(&nisecu);
struct diag_l2_conn *d_conn;
struct diag_l0_device *dl0d = global_dl0d;
int rv;
flag_type flags = 0;
if (!dl0d) {
printf("No global L0. Please select + configure L0 first\n");
return CMD_FAILED;
}
if (global_cfg.L2proto != DIAG_L2_PROT_ISO14230) {
printf("L2 protocol must be iso14230 to use Nissan commands ! try \"set l2protocol\"\n");
return CMD_FAILED;
}
/* Open interface using current L1 proto and hardware */
rv = diag_l2_open(dl0d, global_cfg.L1proto);
if (rv) {
fprintf(stderr, "Open failed for protocol %d on %s\n",
global_cfg.L1proto, dl0d->dl0->shortname);
return CMD_FAILED;
}
if (global_cfg.addrtype) {
flags = DIAG_L2_TYPE_FUNCADDR;
} else {
flags = 0;
}
flags |= (global_cfg.initmode & DIAG_L2_TYPE_INITMASK);
d_conn = diag_l2_StartCommunications(dl0d, global_cfg.L2proto,
flags, global_cfg.speed, global_cfg.tgt, global_cfg.src);
if (d_conn == NULL) {
(void) diag_geterr();
diag_l2_close(dl0d);
printf("L2 StartComms failed\n");
return CMD_FAILED;
}
/* Connected ! */
global_l2_conn = d_conn;
global_state = STATE_CONNECTED;
npstate = NP_NORMALCONN;
update_params();
printf("Connected to ECU !\n");
struct diag_l2_14230 * dlproto; // for bypassing headers
dlproto = (struct diag_l2_14230 *)global_l2_conn->diag_l2_proto_data;
if (dlproto->modeflags & ISO14230_SHORTHDR) {
dlproto->modeflags &= ~ISO14230_LONGHDR; //deactivate long headers
} else {
printf("Short headers not supported by ECU ! Have you \"set addrtype phys\" ?"
"Some stuff will not work.");
}
if (get_ecuid(nisecu.ecuid)) {
printf("Couldn't get ECUID ? Verify settings, connection mode etc.\n");
return CMD_FAILED;
}
printf("ECUID: %s\n", (char *) nisecu.ecuid);
autoselect_keyset();
return CMD_OK;
}
enum cli_retval cmd_spconn(int argc, char **argv) {
(void) argv;
if (argc > 1) {
return CMD_USAGE;
}
if ((npstate != NP_DISC) ||
(global_state != STATE_IDLE)) {
printf("Error : already connected\n");
return CMD_FAILED;
}
nisecu_cleardata(&nisecu);
struct diag_l2_conn *d_conn;
struct diag_l0_device *dl0d = global_dl0d;
int i, rv;
flag_type flags = 0;
struct diag_l2_14230 *dp;
const struct diag_l2_proto *dl2p;
if (!dl0d) {
printf("No global L0. Please select + configure L0 first\n");
return CMD_FAILED;
}
if (global_cfg.L2proto != DIAG_L2_PROT_RAW) {
printf("L2 protocol must start as RAW to communicate with Subaru ECU ! try \"set l2protocol\"\n");
return CMD_FAILED;
}
/* Open interface using current L1 proto and hardware */
rv = diag_l2_open(dl0d, global_cfg.L1proto);
if (rv) {
fprintf(stderr, "Open failed for protocol %d on %s\n",
global_cfg.L1proto, dl0d->dl0->shortname);
return CMD_FAILED;
}
if (global_cfg.addrtype) {
flags = DIAG_L2_TYPE_FUNCADDR;
} else {
flags = 0;
}
flags |= (global_cfg.initmode & DIAG_L2_TYPE_INITMASK);
d_conn = diag_l2_StartCommunications(dl0d, global_cfg.L2proto,
flags, global_cfg.speed, global_cfg.tgt, global_cfg.src);
if (d_conn == NULL) {
(void) diag_geterr();
diag_l2_close(dl0d);
printf("L2 StartComms failed\n");
return CMD_FAILED;
}
//At this point we have a valid RAW connection and need to update d_conn manually to change to ISO14230 with Subaru headers, checksum
//The general initialisation would have set d_conn->diag_link, ->l2proto, ->diag_l2_type, ->diag_l2_srcaddr, ->diag_l2_destaddr,
//->diag_l2_p1 2 2e 3 4 min max, ->tinterval, ->tlast, ->diag_l2_state
//The RAW initialisation would have set diag_serial_settings and d_conn->diag_l2_destaddr and ->diag_l2_srcaddr
printf("L2 RAW detected, changing to L2 ISO14230\n");
global_cfg.L2proto = DIAG_L2_PROT_ISO14230;
for (i=0; l2proto_list[i] ; i++) {
dl2p = l2proto_list[i];
if (dl2p->diag_l2_protocol == global_cfg.L2proto) {
d_conn->l2proto = dl2p;
break;
}
}
rv = diag_calloc(&dp, 1);
if (rv != 0) {
return CMD_FAILED;
}
d_conn->diag_l2_proto_data = (void *)dp;
dp->srcaddr = global_cfg.src;
dp->dstaddr = global_cfg.tgt;
dp->first_frame = 0;
dp->monitor_mode = 0;
d_conn->diag_l2_kb1 = 0x8F;
d_conn->diag_l2_kb2 = 0xEA; //length byte required, address bytes required (ie) 4 byte headers
d_conn->diag_l2_physaddr = global_cfg.src;
dp->state = STATE_ESTABLISHED;
dp->modeflags = 6; //length byte required, address bytes required (ie) 4 byte headers
printf("Change to ISO14230 successful\n");
global_l2_conn = d_conn;
global_state = STATE_CONNECTED;
npstate = NP_NORMALCONN;
update_params();
if(sub_sid81_startcomms()) {
printf("SID 0x81 startCommunications failed. Verify settings, connection mode etc.\n");
global_l2_conn = NULL;
global_state = STATE_IDLE;
npstate = NP_DISC;
return CMD_FAILED;
}
/* Connected ! */
printf("\nConnected to ECU and ready for SSM or SID Commands\n");
if (sub_get_ecuid(nisecu.ecuid)) {
printf("Couldn't get ECUID ? Verify settings, connection mode etc.\n");
return CMD_FAILED;
}
printf("\nECUID: ");
for (i=0; i < 5; i++) {
printf("%02x ", nisecu.ecuid[i]);
}
printf("\n");
return CMD_OK;
}
enum cli_retval cmd_npdisc(UNUSED(int argc), UNUSED(char **argv)) {
if ((npstate == NP_DISC) ||
(global_state == STATE_IDLE)) {
return CMD_OK;
}
if (npstate == NP_NPKCONN) {
printf("\n****** Kernel still running on ECU. Do you want to stop it before disconnecting ?\n"
"n : \t\t No, just disconnect and let kernel run (usually not what you want)\n"
"any other key :\t Yes, stopkernel first (preferred)\n");
char *inp = cli_basic_get_input("> ", stdin);
if (!inp) {
//if user feeds an EOF, don't do anything
return CMD_FAILED;
}
char answer=inp[0];
free(inp);
switch (answer) {
case 'n': //fallthrough
case 'N':
break;
default:
printf("\n\tStopping kernel and rebooting ECU. To avoid the previous prompt,\n"
"\trun 'stopkernel' first.\n");
cmd_stopkernel(1, NULL);
break;
}
}
diag_l2_StopCommunications(global_l2_conn);
diag_l2_close(global_dl0d);
global_l2_conn = NULL;
global_state = STATE_IDLE;
npstate = NP_DISC;
return CMD_OK;
}
enum cli_retval cmd_stopkernel(int argc, UNUSED(char **argv)) {
uint8_t txdata[1];
struct diag_msg nisreq={0}; //request to send
struct diag_msg *rxmsg=NULL; //pointer to the reply
int errval;
if (npstate != NP_NPKCONN) {
return CMD_OK;
}
if (argc != 1) {
return CMD_USAGE;
}
printf("Resetting ECU and closing connection.\n");
txdata[0]=SID_RESET;
nisreq.len=1;
nisreq.data=txdata;
rxmsg=diag_l2_request(global_l2_conn, &nisreq, &errval);
if (rxmsg==NULL) {
return CMD_FAILED;
}
(void) diag_l2_ioctl(global_l2_conn, DIAG_IOCTL_IFLUSH, NULL);
(void) cmd_npdisc(0, NULL);
return CMD_OK;
}
//np 1: try start diagsession, Nissan Repro style +
// accesstimingparams (get limits + setvals)
static int np_1(UNUSED(int argc), UNUSED(char **argv)) {
uint8_t txdata[64]; //data for nisreq
struct diag_msg nisreq={0}; //request to send
struct diag_msg *rxmsg=NULL; //pointer to the reply
int errval;
txdata[0]=0x10;
txdata[1]=0x85;
txdata[2]=0x14;
nisreq.len=3;
nisreq.data=txdata;
rxmsg=diag_l2_request(global_l2_conn, &nisreq, &errval);
if (rxmsg==NULL) {
return CMD_FAILED;
}
if (rxmsg->data[0] != 0x50) {
printf("got bad response : ");
diag_data_dump(stdout, rxmsg->data, rxmsg->len);
printf("\n");
diag_freemsg(rxmsg);
return CMD_FAILED;
}
printf("StartDiagsess: got ");
diag_data_dump(stdout, rxmsg->data, rxmsg->len);
diag_freemsg(rxmsg);
//try accesstimingparam : read limits
txdata[0]=0x83;
txdata[1]=0x0; //read limits
nisreq.len=2;
rxmsg=diag_l2_request(global_l2_conn, &nisreq, &errval);
if (rxmsg==NULL) {
return CMD_FAILED;
}
printf("\nAccesTiming : read limits got ");
diag_data_dump(stdout, rxmsg->data, rxmsg->len);
diag_freemsg(rxmsg);
//try ATP : read settings
txdata[0]=0x83;
txdata[1]=0x02;
nisreq.len=2;
rxmsg=diag_l2_request(global_l2_conn, &nisreq, &errval);
if (rxmsg==NULL) {
return CMD_FAILED;
}
printf("\nAccesTiming : read settings got ");
diag_data_dump(stdout, rxmsg->data, rxmsg->len);
diag_freemsg(rxmsg);
printf("\n");
return CMD_OK;
}
//np 2 :
static int np_2(int argc, char **argv) {
//np 2 <addr> : read 1 byte @ addr, with SID A4
// TX {07 A4 <A0> <A1> <A2> <A3> 04 01 cks}, 9 bytes on bus
// RX {06 E4 <A0> <A1> <A2> <A3> <BB> cks}, 8 bytes
// total traffic : 17 bytes for 1 rx'd byte - very slow
//printf("Attempting to read 1 byte @ 000000:\n");
uint8_t txdata[64]; //data for nisreq
uint32_t addr;
struct diag_msg nisreq={0}; //request to send
struct diag_msg *rxmsg=NULL; //pointer to the reply
int errval;
if (argc != 3) {
printf("usage: npt 2 <addr>: read 1 byte @ <addr>\n");
return CMD_USAGE;
}
if (sscanf(argv[2], "%x", &addr) != 1) {
printf("Did not understand %s\n", argv[2]);
return CMD_USAGE;
}
txdata[0]=0xA4;
txdata[4]= (uint8_t) (addr & 0xFF);
txdata[3]= (uint8_t) (addr >> 8) & 0xFF;
txdata[2]= (uint8_t) (addr >> 16) & 0xFF;
txdata[1]= (uint8_t) (addr >> 24) & 0xFF;
txdata[5]=0x04; //TXM
txdata[6]=0x01; //NumResps
nisreq.len=7;
nisreq.data=txdata;
rxmsg=diag_l2_request(global_l2_conn, &nisreq, &errval);
if (rxmsg==NULL) {
return CMD_FAILED;
}
if ((rxmsg->data[0] != 0xE4) || (rxmsg->len != 6)) {
printf("got bad A4 response : ");
diag_data_dump(stdout, rxmsg->data, rxmsg->len);
printf("\n");
diag_freemsg(rxmsg);
return CMD_FAILED;
}
printf("Got: 0x%02X\n", rxmsg->data[5]);
diag_freemsg(rxmsg);
return CMD_OK;
}
/** np 5 : fast dump <len> bytes @<start> to already-opened <outf>;
* uses fast read technique (receive from L1 direct)
*
* return CMD_* , caller must close outf
*/
static int dump_fast(FILE *outf, const uint32_t start, uint32_t len) {
//SID AC + 21 technique.
// AC 81 {83 GGGG} {83 GGGG} ... to load addresses, (5*n + 4) bytes on bus
// RX: {EC 81}, 4 bytes
// TX: {21 81 04 01} to dump data (6 bytes)
// RX: {61 81 <n*data>} (4 + n) bytes.
// Total traffic : (6*n + 18) bytes on bus for <n> bytes RX'd
struct diag_msg nisreq={0}; //request to send
uint8_t txdata[64]; //data for nisreq
int errval;
int retryscore=100; //successes increase this up to 100; failures decrease it.
uint8_t hackbuf[70];
int extra; //extra bytes to purge
uint32_t addr, nextaddr, maxaddr;
unsigned long total_chron;
nextaddr = start;
maxaddr = start + len - 1;
if (!outf) {
return CMD_FAILED;
}
nisreq.data=txdata;
total_chron = diag_os_getms();
while (retryscore >0) {
unsigned int linecur=0; //count from 0 to 11 (12 addresses per request)
int txi; //index into txbuf for constructing request
printf("Starting dump from 0x%08X to 0x%08X.\n", nextaddr, maxaddr);
txdata[0]=0xAC;
txdata[1]=0x81;
nisreq.len = 2; //AC 81 : 2 bytes so far
txi=2;
linecur = 0;
unsigned long t0, chrono;
unsigned chron_cnt = 0; //how many bytes between refreshes
t0 = diag_os_getms();
for (addr=nextaddr; addr <= maxaddr; addr++) {
txdata[txi++]= 0x83; //field type
txdata[txi++]= (uint8_t) (addr >> 24) & 0xFF;
txdata[txi++]= (uint8_t) (addr >> 16) & 0xFF;
txdata[txi++]= (uint8_t) (addr >> 8) & 0xFF;
txdata[txi++]= (uint8_t) (addr & 0xFF);
nisreq.len += 5;
linecur += 1;
//request 12 addresses at a time, or whatever's left at the end
if ((linecur != 0x0c) && (addr != maxaddr)) {
continue;
}
unsigned curspeed, tmin, tsec;
chron_cnt += linecur;
chrono = diag_os_getms() - t0;
if (chrono > 200) {
//limit update rate
curspeed = 1000 * (chron_cnt) / chrono; //avg B/s
if (!curspeed) {
curspeed += 1;
}
tsec = ((maxaddr - addr) / curspeed) % 9999;
tmin = tsec / 60;
tsec = tsec % 60;
printf("\rreading @ 0x%08X (%3u %%, %5u B/s, ~ %3u:%02u remaining ", nextaddr,
(unsigned) 100 * (maxaddr - addr) / len, curspeed, tmin, tsec);
fflush(stdout);
chron_cnt = 0;
t0 = diag_os_getms();
}
int i, rqok;
//send the request "properly"
rqok = diag_l2_send(global_l2_conn, &nisreq);
if (rqok) {
printf("\nhack mode : bad l2_send\n");
retryscore -= 25;
diag_os_millisleep(300);
(void) diag_l2_ioctl(global_l2_conn, DIAG_IOCTL_IFLUSH, NULL);
break; //out of for()
}
rqok=0; //default to fail
//and get a response; we already know the max expected length:
// 0xEC 0x81 + 2 (short hdr) or +4 (full hdr).
// we'll request just 4 bytes so we return very fast;
// We should find 0xEC if it's in there no matter what kind of header.
// We'll "purge" the next bytes when we send SID 21
errval=diag_l1_recv(global_l2_conn->diag_link->l2_dl0d,
hackbuf, 4, (unsigned) (25 + nparam_rxe.val));
if (errval == 4) {
//try to find 0xEC in the first bytes:
for (i=0; i<=3; i++) {
if (hackbuf[i] == 0xEC) {
rqok=1;
break;
}
}
}
if (!rqok) {
printf("\nhack mode : bad AC response %02X %02X\n", hackbuf[0], hackbuf[1]);
retryscore -= 25;
diag_os_millisleep(300);
(void) diag_l2_ioctl(global_l2_conn, DIAG_IOCTL_IFLUSH, NULL);
break; //out of for()
}
//Here, we're guaranteed to have found 0xEC in the first 4 bytes we got. But we may
//need to "purge" some extra bytes on the next read
// hdr0 (hdr1) (hdr2) 0xEC 0x81 ck
//
extra = (3 + i - errval); //bytes to purge. I think the formula is ok
extra = (extra < 0) ? 0: extra; //make sure >=0
//Here, we sent a AC 81 83 ... 83... request that was accepted.
//We need to send 21 81 04 01 to get the data now
txdata[0]=0x21;
txdata[1]=0x81;
txdata[2]=0x04;
txdata[3]=0x01;
nisreq.len=4;
rqok=0; //default to fail
//send the request "properly"
if (diag_l2_send(global_l2_conn, &nisreq)) {
printf("l2_send() problem !\n");
retryscore -=25;
diag_os_millisleep(300);
(void) diag_l2_ioctl(global_l2_conn, DIAG_IOCTL_IFLUSH, NULL);
break; //out of for ()
}
//and get a response; we already know the max expected length:
//61 81 [2+linecur] + max 4 (header+cks) = 8+linecur
//but depending on the previous message there may be extra
//bytes still in buffer; we already calculated how many.
//By requesting (extra) + 4 with a short timeout, we'll return
//here very quickly and we're certain to "catch" 0x61.
errval=diag_l1_recv(global_l2_conn->diag_link->l2_dl0d,
hackbuf, extra + 4, (unsigned) (25 + nparam_rxe.val));
if (errval != extra+4) {
retryscore -=25;
diag_os_millisleep(300);
(void) diag_l2_ioctl(global_l2_conn, DIAG_IOCTL_IFLUSH, NULL);
break; //out of for ()
}
//try to find 0x61 in the first bytes:
for (i=0; i<errval; i++) {
if (hackbuf[i] == 0x61) {
rqok=1;
break;
}
}
//we now know where the real data starts so we can request the
//exact number of bytes remaining. Now, (errval - i) is the number
//of packet bytes already read including 0x61, ex.:
//[XX XX 61 81 YY YY ..] : i=2 and errval =5 means we have (5-2)=3 bytes
// of packet data (61 81 YY)
// Total we need (2 + linecur) packet bytes + 1 cksum
// So we need to read (2+linecur+1) - (errval-i) bytes...
// Plus : we need to dump those at the end of what we already got !
extra = (3 + linecur) - (errval - i);
if (extra<0) {
printf("\nhack mode : problem ! extra=%d\n",extra);
extra=0;
} else {
errval=diag_l1_recv(global_l2_conn->diag_link->l2_dl0d,
&hackbuf[errval], extra, (unsigned) (25 + nparam_rxe.val));
}
if (errval != extra) { //this should always fit...
rqok=0;
}
if (!rqok) {
//either negative response or not enough data !
printf("\nhack mode : bad 61 response %02X %02X, i=%02X extra=%02X ev=%02X\n",
hackbuf[i], hackbuf[i+1], i, extra, errval);
diag_os_millisleep(300);
(void) diag_l2_ioctl(global_l2_conn, DIAG_IOCTL_IFLUSH, NULL);
retryscore -= 25;
break; //out of for ()
}
//and verify checksum. [i] points to 0x61;
if (hackbuf[i+2+linecur] != diag_cks1(&hackbuf[i-1], 3+linecur)) {
//this checksum will not work with long headers...
printf("\nhack mode : bad 61 CS ! got %02X\n", hackbuf[i+2+linecur]);
diag_data_dump(stdout, &hackbuf[i], linecur+3);
diag_os_millisleep(300);
(void) diag_l2_ioctl(global_l2_conn, DIAG_IOCTL_IFLUSH, NULL);
retryscore -=20;
break; //out of for ()
}
//We can now dump this to the file...
if (fwrite(&(hackbuf[i+2]), 1, linecur, outf) != linecur) {
printf("Error writing file!\n");
retryscore -= 101; //fatal, sir
break; //out of for ()
}
nextaddr += linecur; //if we crash, we can resume starting at nextaddr
linecur=0;
//success: allow us more errors
retryscore = (retryscore > 95)? 100:(retryscore+5);
//and reset tx template + sub-counters
txdata[0]=0xAc;
txdata[1]=0x81;
nisreq.len=2;
txi=2;
} //for
if (addr <= maxaddr) {
//the for loop didn't complete;
//(if succesful, addr == maxaddr+1 !!)
printf("\nRetry score: %d\n", retryscore);
} else {
printf("\nFinished! ~%lu Bps\n", 1000*(maxaddr - start)/(diag_os_getms() - total_chron));
break; //leave while()
}
} //while retryscore>0
if (retryscore <= 0) {
printf("Too many errors, no more retries @ addr=%08X.\n", start);
return CMD_FAILED;
}
return CMD_OK;
}