-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
nis_backend.c
442 lines (372 loc) · 9.92 KB
/
nis_backend.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
/*
* nisprog - Nissan ECU communications utility
*
* Copyright (c) 2014-2016 fenugrec
*
* Licensed under GPLv3
*
* back-end functions used by CLI commands, for Nissan ECUs
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "stypes.h"
#include "diag.h"
#include "diag_l1.h"
#include "diag_l2.h"
#include "diag_iso14230.h" //for NRC decoding
#include "nisprog.h"
#include "nis_backend.h"
#include "nissutils/cli_utils/nislib.h"
#define CURFILE "nis_backend.c" //HAAAX
/** Decode negative response code into a short error string.
*
* rxdata[] must contain at least 3 bytes, "7F <SID> <NRC>"
* returns a static char * that must not be free'd !
*/
static const char *decode_nrc(uint8_t *rxdata);
/* algo1.
* ( NPT_DDL2 algo (with key-in-ROM) ... niskey1.c )
*/
void genkey1(const uint8_t *seed8, uint32_t m, uint8_t *key) {
uint32_t seed = reconst_32(seed8);
write_32b(enc1(seed, m), key); //write key in buffer.
return;
}
/** Encrypt with the kline_at algo... niskey2.c
* writes 4 bytes in buffer *key
*/
static void genkey2(const uint8_t *seed8, uint8_t *key) {
uint32_t seed, ecx, xorloops;
int ki;
const uint32_t keytable[]={0x14FA3579, 0x27CD3964, 0x1777FE32, 0x9931AF12,
0x75DB3A49, 0x19294CAA, 0x0FF18CD76, 0x788236D,
0x5A6F7CBB, 0x7A992254, 0x0ADFD5414, 0x343CFBCB,
0x0C2F51639, 0x6A6D5813, 0x3729FF68, 0x22A2C751};
seed = reconst_32(seed8);
ecx = (seed & 1)<<6 | (seed>>9 & 1)<<4 | (seed>>1 & 1)<<3;
ecx |= (seed>>11 & 1)<<2 | (seed>>2 & 1)<<1 | (seed>>5 & 1);
ecx += 0x1F;
if (((int32_t) ecx) <= 0) {
printf("problem !!\n");
return;
}
ki = (seed & 1)<<3 | (seed>>1 & 1)<<2 | (seed>>2 & 1)<<1 | (seed>>9 & 1);
//printf("starting xorloop with ecx=0x%0X, ki=0x%0X\n", ecx, ki);
for (xorloops=0; xorloops < ecx; xorloops++) {
if (seed & 0x80000000) {
seed += seed;
seed ^= keytable[ki];
} else {
seed += seed;
}
}
//here, the generated key is in "seed".
write_32b(seed, key);
return;
}
int get_ecuid(u8 *dest) {
struct diag_msg nisreq={0}; //request to send
struct diag_msg *rxmsg=NULL; //pointer to the reply
uint8_t txdata[64]; //data for nisreq
int errval;
if (npstate == NP_DISC) {
printf("Not connected to ECU\nTry \"nc\" first\n");
return -1;
}
nisreq.data=txdata;
//request ECUID
txdata[0]=0x1A;
txdata[1]=0x81;
nisreq.len=2;
rxmsg=diag_l2_request(global_l2_conn, &nisreq, &errval);
if (rxmsg==NULL) {
return -1;
}
if ((rxmsg->len < 7) || (rxmsg->data[0] != 0x5A)) {
printf("got bad 1A response : ");
diag_data_dump(stdout, rxmsg->data, rxmsg->len);
printf("\n");
diag_freemsg(rxmsg);
return -1;
}
memcpy(dest, rxmsg->data + 2, 5); //skip 0x5A 0x31
dest[5]=0; //null-terminate
diag_freemsg(rxmsg);
return 0;
}
/*
* keyalg = 1 for "algo 1" (NPT_DDL2) + scode: widespread)
* keyalg = 2 for alternate (genkey2, KLINE_AT)
*
* @return 0 if successful
*/
int sid27_unlock(int keyalg, uint32_t scode) {
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]=0x27;
txdata[1]=0x01; //RequestSeed
nisreq.len=2;
nisreq.data=txdata;
rxmsg=diag_l2_request(global_l2_conn, &nisreq, &errval);
if (rxmsg==NULL) {
return -1;
}
if ((rxmsg->len < 6) || (rxmsg->data[0] != 0x67)) {
printf("got bad 27 01 response : ");
if (rxmsg->data[0] == 0x7F) {
printf("%s\n", decode_nrc(rxmsg->data));
} else {
diag_data_dump(stdout, rxmsg->data, rxmsg->len);
printf("\n");
}
diag_freemsg(rxmsg);
return -1;
}
txdata[0]=0x27;
txdata[1]=0x02; //SendKey
switch (keyalg) {
case 1:
genkey1(&rxmsg->data[2], scode, &txdata[2]); //write key to txdata buffer
//printf("; using NPT_DDL algo (scode=0x%08X), ", scode);
break;
case 2:
default:
genkey2(&rxmsg->data[2], &txdata[2]); //write key to txdata buffer
//printf("; using KLINE_AT algo, ");
break;
}
diag_freemsg(rxmsg);
printf("\n");
nisreq.len=6; //27 02 K K K K
rxmsg=diag_l2_request(global_l2_conn, &nisreq, &errval);
if (rxmsg==NULL) {
return -1;
}
if (rxmsg->data[0] != 0x67) {
printf("got bad 27 02 response : ");
if (rxmsg->data[0] == 0x7F) {
printf("%s\n", decode_nrc(rxmsg->data));
} else {
diag_data_dump(stdout, rxmsg->data, rxmsg->len);
printf("\n");
}
diag_freemsg(rxmsg);
return -1;
}
printf("SID27:SUXXESS !!\n");
diag_freemsg(rxmsg);
return 0;
}
/* ret 0 if ok
*
* Assumes everything is ok (conn state, etc)
*/
int sid3480(void) {
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]=0x34;
txdata[1]=0x80;
nisreq.len=2;
nisreq.data=txdata;
rxmsg=diag_l2_request(global_l2_conn, &nisreq, &errval);
if (rxmsg==NULL) {
return -1;
}
if (rxmsg->data[0] != 0x74) {
printf("got bad 34 80 response : ");
diag_data_dump(stdout, rxmsg->data, rxmsg->len);
printf("\n");
diag_freemsg(rxmsg);
return -1;
}
diag_freemsg(rxmsg);
return 0;
}
/* transfer payload from *buf
* len must be multiple of 32
* Caller must have encrypted the payload
* ret 0 if ok
*/
int sid36(uint8_t *buf, uint32_t len) {
uint8_t txdata[64]; //data for nisreq
struct diag_msg nisreq={0}; //request to send
int errval;
uint16_t blockno;
uint16_t maxblocks;
len &= ~0x1F;
if (!buf || !len) {
return -1;
}
blockno = 0;
maxblocks = (len / 32) - 1;
txdata[0]=0x36;
//txdata[1] and [2] is the 16bit block #
txdata[3] = 0x20; //block length; ignored by ECU
nisreq.data=txdata;
nisreq.len= 4 + 32;
for (; len > 0; len -= 32, blockno += 1) {
uint8_t rxbuf[10]; //can't remember what the actual sid 36 response looks like
txdata[1] = blockno >> 8;
txdata[2] = blockno & 0xFF;
memcpy(&txdata[4], buf, 32);
buf += 32;
//rxmsg=diag_l2_request(global_l2_conn, &nisreq, &errval);
errval = diag_l2_send(global_l2_conn, &nisreq);
if (errval) {
printf("l2_send error!\n");
return -1;
}
errval = diag_l1_recv(global_l2_conn->diag_link->l2_dl0d, rxbuf, 3, 50);
if (errval < 3) {
printf("no response @ blockno %X\n", (unsigned) blockno);
(void) diag_l2_ioctl(global_l2_conn, DIAG_IOCTL_IFLUSH, NULL);
return -1;
}
if (rxbuf[0] & 0x80) {
printf("Problem: ECU responding with long headers ?\n");
return -1;
}
if (rxbuf[1] != 0x76) {
printf("got bad 36 response : ");
if (rxbuf[1] == 0x7F) {
printf("%s\n", decode_nrc(&rxbuf[1]));
} else {
diag_data_dump(stdout, rxbuf, errval);
printf("\n");
}
(void) diag_l2_ioctl(global_l2_conn, DIAG_IOCTL_IFLUSH, NULL);
return -1;
}
printf("\rSID36 block 0x%04X/0x%04X done",
(unsigned) blockno, (unsigned) maxblocks);
fflush(stdout);
}
printf("\n");
fflush(stdout);
return 0;
}
//send SID 37 transferexit request, ret 0 if ok
int sid37(uint16_t cks) {
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]=0x37;
txdata[1]=cks >> 8;
txdata[2]=cks & 0xFF;
nisreq.len=3;
nisreq.data=txdata;
printf("sid37: sending ");
diag_data_dump(stdout, txdata, 3);
printf("\n");
rxmsg=diag_l2_request(global_l2_conn, &nisreq, &errval);
if (rxmsg==NULL) {
return -1;
}
if (rxmsg->data[0] != 0x77) {
printf("got bad 37 response : ");
if (rxmsg->data[0] == 0x7F) {
printf("%s\n", decode_nrc(rxmsg->data));
} else {
diag_data_dump(stdout, rxmsg->data, rxmsg->len);
printf("\n");
}
diag_freemsg(rxmsg);
return -1;
}
diag_freemsg(rxmsg);
return 0;
}
/* RAMjump, takes care of SIDs BF 00 + BF 01
* ret 0 if ok
*/
int sidBF(void) {
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]=0xBF;
txdata[1]=0;
nisreq.len=2;
nisreq.data=txdata;
/* BF 00 : RAMjumpCheck */
rxmsg=diag_l2_request(global_l2_conn, &nisreq, &errval);
if (rxmsg==NULL) {
return -1;
}
if (rxmsg->data[0] != 0xFF) {
printf("got bad BF 00 response : ");
diag_data_dump(stdout, rxmsg->data, rxmsg->len);
printf("\n");
diag_freemsg(rxmsg);
return -1;
}
diag_freemsg(rxmsg);
/* BF 01 : RAMjumpCheck */
txdata[1] = 1;
rxmsg=diag_l2_request(global_l2_conn, &nisreq, &errval);
if (rxmsg==NULL) {
return -1;
}
if (rxmsg->data[0] != 0xFF) {
printf("got bad BF 01 response : ");
diag_data_dump(stdout, rxmsg->data, rxmsg->len);
printf("\n");
diag_freemsg(rxmsg);
return -1;
}
diag_freemsg(rxmsg);
return 0;
}
#define NRC_STRLEN 80 //too small and we get an assert() failure in smartcat() !!!
/** Return string for neg response code
*
* rxdata must point to the data frame (no headers), i.e. 0x7F <SID> <NRC>
*/
static const char *decode_nrc(uint8_t *rxdata) {
struct diag_msg tmsg;
static char descr[NRC_STRLEN]="";
u8 nrc = rxdata[2];
//XXX TODO : move this to nislib common defs someday
//XXX TODO : determine which of these is SID dependant
#define C2_NRC_BAD_SID36_SEQ 0x90
#define C2_NRC_BAD_SID37_CKS 0x91
#define C2_NRC_LOWSYSV 0x92
#define C2_NRC_ADCG 0x93 //SID 27, "Additional charge condition" = wtf
#define C2_NRC_ENGRUN 0x94 //SID 27, fENGRUN - engine running
#define C2_NRC_LOADSW 0x95 //SID 27, electrical loads present
// 1) try mfg-specific NRCs
switch (nrc) {
case C2_NRC_BAD_SID36_SEQ:
return "Bad SID 36 block sequence / length";
break;
case C2_NRC_BAD_SID37_CKS:
return "Bad SID 36/37 payload checksum";
break;
case C2_NRC_LOWSYSV:
return "Low battery voltage";
break;
case C2_NRC_ADCG:
return "Additional charge condition (?)";
break;
case C2_NRC_ENGRUN:
return "Engine running";
break;
case C2_NRC_LOADSW:
return "Electrical loads active";
break;
default:
// 2) Try Standard ISO14230 NRC
tmsg.data = rxdata;
tmsg.len = 3; //assume rxdata contains a "7F <SID> <NRC>" message
(void) diag_l3_iso14230_decode_response(&tmsg, descr, NRC_STRLEN);
return descr;
break;
}
return descr;
}