-
Notifications
You must be signed in to change notification settings - Fork 2
/
Dump.java
168 lines (126 loc) · 4.77 KB
/
Dump.java
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
package DTCController;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* Class to read, write and find DTC info from a dump
*/
public class Dump {
public final String ERROR_STR = "Cannot find DTC Table, please contribute to this project to add new platform.";
public static final int DUMP_SIZE = 2048*1024; // 2MBytes
public static final int MAP_ADD = 0x1C0000;
public static final int MAP_SIZE = 0x3DF78;
public static final int SWT_REF = MAP_ADD + 0x10;
public static final int A2L_REF = MAP_ADD + 0x1A;
public static final int MAX_DTC = 1024;
public static int CLASS_NUMBER = 30;
byte[] memory;
String software;
String project;
public Dump(String fileName) throws IOException {
Path p = Paths.get(fileName);
memory = Files.readAllBytes(p);
StringBuffer sft = new StringBuffer();
for(int i=0;i<10;i++)
sft.append((char)memory[SWT_REF+i]);
software = sft.toString();
StringBuffer a2l = new StringBuffer();
for(int i=0;i<8;i++)
a2l.append((char)memory[A2L_REF+i]);
project = a2l.toString();
System.out.println(fileName + ":" + software + "/" + project);
}
public void write(String fileName) throws IOException {
Path p = Paths.get(fileName);
Files.write(p,memory);
System.out.println("Write: " + fileName);
}
public static void setMemory(byte[] memory,int addr,int b) {
int old = ((int)memory[addr]) & 0xFF;
if( old!=b ) {
System.out.println(String.format("Dump.setMemory(): 0x%06X %d => %d",addr,old,b));
}
memory[addr] = (byte)b;
}
public DTCInfo findDTCInfo() throws IOException {
DTCInfo ret = new DTCInfo();
// Known software
// Please add know project there
if (project.equals("C35374A_")) {
// 206 1.6 HDI 110cv FAP
ret.nbDTC = 195; // Number of DTC
ret.classAddr = 0x1C6734; // Addr of DSM_ClaDfp_ACCDPresAna_C
ret.defClassAddr = 0x1C5CCC; // Addr of DSM_Class1Mil_C
} else if (project.startsWith("C")) {
// PSA strategy, we assume that:
// First code of the codeTable is P0530
// This code corresponds to DSM_CDKDfp_ACCDPresAna_C (AirConditioningComponentDriver Pressure default)
// Last code of the codeTable is P1621
// This code corresponds to DSM_CDKDfp_WdCom_C (ECU failure)
// Characteristics are sorted by alphabetic order (case sensitive) when damos is generated
// Try to find an occurrence of 0x1621 in reverse order
byte[] p1621 = new byte[]{0x16,0x21};
ret.classAddr = rSearch(MAP_ADD+MAP_SIZE-p1621.length,p1621) + 2;
if(ret.classAddr<0)
throw new IOException(ERROR_STR);
byte[] p0530 = new byte[]{0x05,0x30};
int firstCode = rSearch(ret.classAddr,p0530);
if(firstCode<0)
throw new IOException(ERROR_STR);
ret.nbDTC = (ret.classAddr - firstCode) / 2;
if(ret.nbDTC>MAX_DTC)
throw new IOException(ERROR_STR);
// Start of DTC class definition 1 (from orig file)
ret.defClassAddr = search(MAP_ADD,new byte[]{1,1,0,2,3});
} else {
throw new IOException(ERROR_STR);
}
ret.codeAddr = ret.classAddr - ret.nbDTC*2;
ret.faultPathAddr = ret.codeAddr - ret.nbDTC*8;
ret.envAddr = ret.classAddr + ret.nbDTC;
System.out.println(ret);
return ret;
}
@Override
public String toString() {
return "Dump{" +
"memory=" + memory.length +
", software='" + software + '\'' +
", project='" + project + '\'' +
'}';
}
private boolean match(int add, byte[] pattern) {
boolean equal = true;
int i = 0;
while(equal && i<pattern.length) {
equal = pattern[i] == memory[add + i];
i++;
}
return equal;
}
private int search(int startAdd,byte[] pattern) {
int add = startAdd;
boolean found = false;
while(!found && add<MAP_ADD+MAP_SIZE-pattern.length) {
found = match(add, pattern);
if (!found) add++;
}
if(!found)
return -1;
else
return add;
}
private int rSearch(int startAdd,byte[] pattern) {
int add = startAdd;
boolean found = false;
while(!found && add>=MAP_ADD) {
found = match(add, pattern);
if (!found) add--;
}
if(!found)
return -1;
else
return add;
}
}