-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQMSL-QCNTool.cpp
336 lines (323 loc) · 8.46 KB
/
QMSL-QCNTool.cpp
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
#include <CLI11.hpp>
#include "QCLoader.h"
#include <iostream>
#include <String>
#include <chrono>
#include <codecvt>
#include <locale>
#include <ctime>
#include <iomanip>
#include <fstream>
using namespace std;
void SetLibraryMode()
{
std::cout << "\nSetting QPST mode...";
QLIB_SetLibraryMode(0);
std::cout << " OK";
}
bool ConnectPort(DiagInfo &info)
{
std::cout << "\nConnecting to phone...";
info.hndl = QLIB_ConnectServer(info.portnum);
unsigned int a = QLIB_IsPhoneConnected(info.hndl);
if (a)
{
std::cout << " OK";
unsigned long _iMSM_HW_Version = 0;
unsigned long _iMobModel = 0;
char _sMobSwRev[512];
char _sModelStr[512];
unsigned char bOk;
bOk = QLIB_DIAG_EXT_BUILD_ID_F(info.hndl, &_iMSM_HW_Version, &_iMobModel, _sMobSwRev, _sModelStr);
if (bOk)
{
std::cout << "\nMSM-HW ver : ";
std::cout << std::to_string(_iMSM_HW_Version) << std::endl;
std::cout << "\nMobModel : ";
std::cout << std::to_string(_iMobModel) << std::endl;
std::cout << "\nSoftware : ";
std::cout << _sMobSwRev << std::endl;
}
return true;
}
else
{
std::cout << " error";
std::cout << "\nPlease select a valid diag port!";
return false;
}
}
bool SetSIMDual(DiagInfo &info, bool dual)
{
std::cout << "\nSet Multi Sim...";
if (dual)
{
unsigned char res = QLIB_NV_SetTargetSupportMultiSIM(info.hndl, true);
if (res)
{
std::cout << " 2";
return true;
}
else
{
std::cout << " error";
return false;
}
}
else
{
if (QLIB_NV_SetTargetSupportMultiSIM(info.hndl, false))
{
std::cout << " 1";
return true;
}
else
{
std::cout << " error";
return false;
}
}
}
bool SyncEFS(DiagInfo &info)
{
std::cout << "\nSyncing EFS...";
unsigned char b = 0;
unsigned char b2 = 0;
b = 47;
b2 = 4;
try
{
QLIB_EFS2_SyncWithWait(info.hndl, &b, 2000, &b2);
}
catch (...)
{
std::cout << " error";
std::cout << "\nCan not sync EFS";
return false;
}
std::cout << " OK";
return true;
}
bool RebootNormal(DiagInfo &info)
{
std::cout << "Rebooting phone...";
QLIB_DIAG_CONTROL_F(info.hndl, MODE_OFFLINE_D_F);
Sleep(2000);
QLIB_DIAG_CONTROL_F(info.hndl, MODE_RESET_F);
std::cout << " OK";
return true;
}
typedef struct
{
std::wstring imei;
std::wstring tac;
std::wstring fac;
std::wstring snr;
std::wstring svn;
std::wstring luhnCode;
} Imei_Info;
string ReadIMEI(DiagInfo &info, int index)
{
unsigned char array[128];
std::wstring array2[15];
int array3[15];
unsigned short num1 = 4;
unsigned char res = QLIB_DIAG_NV_READ_EXT_F(info.hndl, NV_UE_IMEI_I, array, index, 128, &num1);
if (!res)
return "000000000000000";
int num = 0;
for (int i = 1; i <= 8; i++)
{
if (i != 8)
{
array3[num] = static_cast<int>(array[i]);
array3[num] &= 240;
array3[num] >>= 4;
array3[num + 1] = static_cast<int>(array[i + 1] & 15);
}
else
{
array3[num] = static_cast<int>(array[i]);
array3[num] &= 240;
array3[num] >>= 4;
}
num += 2;
}
Imei_Info imeiinfo;
for (int j = 0; j < 15; j++)
{
array2[j] = std::to_wstring(array3[j]);
if (j < 6)
{
imeiinfo.tac += array2[j];
}
else if (j >= 6 && j <= 7)
{
imeiinfo.fac += array2[j];
}
else if (j >= 7 && j <= 13)
{
imeiinfo.snr += array2[j];
}
}
imeiinfo.luhnCode = array2[14];
imeiinfo.imei = imeiinfo.tac + imeiinfo.fac + imeiinfo.snr + imeiinfo.luhnCode;
if (imeiinfo.imei.size() != 15)
{
return "000000000000000";
}
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
std::string utf8_imei = myconv.to_bytes(imeiinfo.imei);
return utf8_imei;
}
bool SendSPC(DiagInfo &info, const std::string &SPC)
{
std::cout << "\nSending SPC...";
unsigned char piSPC_Result;
// 将SPC复制到unsigned char数组
unsigned char test2[6];
std::memcpy(test2, SPC.c_str(), SPC.size());
if (QLIB_DIAG_SPC_F(info.hndl, test2, &piSPC_Result))
{
std::cout << " OK";
// 假设ReadIMEI返回std::string类型
std::string IMEI1 = ReadIMEI(info, 0);
std::string IMEI2 = ReadIMEI(info, 1);
std::cout << "\nPhone IMEI1 : ";
std::cout << IMEI1;
std::cout << "\nPhone IMEI2 : ";
std::cout << IMEI2;
return true;
}
else
{
std::cout << " error";
}
return false;
}
bool ConnectDevice(DiagInfo &info)
{
SetLibraryMode();
if (!ConnectPort(info))
{
return false;
}
if (!SendSPC(info, "000000"))
{
return false;
}
if (!SetSIMDual(info, !(ReadIMEI(info, 0) == ReadIMEI(info, 1))))
{
return false;
}
return true;
}
std::string createFilePath(const std::string ¤t_path)
{
if (current_path.empty())
{
return "";
}
time_t time_seconds = time(0);
struct tm now_time;
localtime_s(&now_time, &time_seconds);
std::ostringstream timestamp;
timestamp << std::put_time(&now_time, "%Y%m%d%H%M%S");
std::string file_path = current_path + "/" + "QCN_" + timestamp.str() + ".qcn";
std::replace(file_path.begin(), file_path.end(), ':', '_');
std::replace(file_path.begin(), file_path.end(), ' ', '_');
return file_path;
}
int main(int argc, char **argv)
{
CLI::App app{"QCNTool"};
app.set_version_flag("--version", "1.5.0", "A tool to download/flash qcn from/to your phone");
app.set_help_flag("-h,--help", "a tool to download/flash qcn from/to your phone");
bool writeqcn = false;
bool readqcn = false;
int targetport = 0;
std::string targetpath;
app.add_flag("--write, -w", writeqcn, "Write qcn to your phone");
app.add_flag("--read, -r", readqcn, "Backup qcn from your phone");
app.add_option("--port, -p", targetport, "Set 901D port number")->required();
app.add_option("--file, -f", targetpath, "QCN file path");
CLI11_PARSE(app, argc, argv);
// 创建DiagInfo实例并赋值
DiagInfo info;
info.portnum = targetport;
if (writeqcn == readqcn)
{
std::cout << "Invalid function quest";
return 1;
}
std::cout << "a small free tool to flash/backup qcn into/from your phone\n";
if (!ConnectDevice(info))
{
QLIB_DisconnectServer(info.hndl);
return 1;
}
if (writeqcn)
{
if (targetpath == "")
{
std::cout << "please input file path";
QLIB_DisconnectServer(info.hndl);
return 1;
}
std::cout << "\nLoading Data File...";
int get1 = -1;
int get2 = -1;
if (!QLIB_NV_LoadNVsFromQCN(info.hndl, targetpath.c_str(), &get1, &get2))
{
std::cout << " error";
QLIB_DisconnectServer(info.hndl);
return 1;
}
else
{
std::cout << " OK";
std::cout << "\nWriting Data File to phone...";
int res2;
if (!QLIB_NV_WriteNVsToMobile(info.hndl, &res2))
{
std::cout << " error";
QLIB_DisconnectServer(info.hndl);
return 1;
}
else
{
std::cout << " OK";
}
}
}
if (readqcn)
{
if (targetpath.empty())
{
targetpath = createFilePath(argv[0]);
}
else
{
std::cerr << "file path should be null" << std::endl;
return 1;
}
int renas2;
char *inputpath = const_cast<char *>(targetpath.c_str());
std::cout << "\nReading QCN from phone...";
if (!QLIB_BackupNVFromMobileToQCN(info.hndl, inputpath, &renas2))
{
std::cout << " error";
QLIB_DisconnectServer(info.hndl);
return 1;
}
else
{
std::cout << " OK";
std::cout << "\nBackup file : ";
std::cout << targetpath;
std::cout << "\n";
}
}
QLIB_DisconnectServer(info.hndl);
return 0;
}