forked from crazy4linux/SP_Flash_Tool_src_v3.1113.0.189
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBromDLLWrapper.cpp
493 lines (390 loc) · 14 KB
/
BromDLLWrapper.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
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
// DllWrapper.cpp
// : Wrapper class for BromDll.dll
//
#include "BromDllWrapper.h"
#include "BromDllProcAddr.h"
CBromDllWrapper::CBromDllWrapper(void)
{
m_hBromdll = NULL;
m_DAHandle = NULL;
m_DLHandle = NULL;
m_pCbFlashTool = NULL;
m_pCbDownload = NULL;
}
CBromDllWrapper::~CBromDllWrapper(void)
{
if (m_hBromdll){
//Free resource
if (m_DAHandle) {
::gps_DA_Destroy(m_hBromdll, m_DAHandle);
m_DAHandle = NULL;
}
if (m_DLHandle) {
::gps_DL_Destroy(m_hBromdll, m_DLHandle);
m_DLHandle = NULL;
}
::FreeLibrary(m_hBromdll);
m_hBromdll = NULL;
}
}//END_OF Dtor
// -----------------------------------------------------------------------------
WRAPPER_RETURN CBromDllWrapper::fnLoadBromDLL(const char * pszFullPath)
{
m_hBromdll = LoadLibrary(pszFullPath);
if( NULL == m_hBromdll ){
return ERROR_DLL_NOT_LOAD;
}
return COMPLETE_SUCCESS;
}//END_OF fnLoadBromDLL()
WRAPPER_RETURN CBromDllWrapper::fnLoadDA(const char * pszFullPath)
{
if( !m_hBromdll ){
return ERROR_DLL_NOT_LOAD;
}
WRAPPER_RETURN returnValue;
//- 1. Create DA_HANDLE
if (COMPLETE_SUCCESS != (returnValue = ::gps_DA_Create(m_hBromdll, m_DAHandle))) {
return returnValue;
}
if (NULL == m_DAHandle) {
//Should have returned above
return ERROR_FUNC_INTERNAL_LOGIC;
}
//- 2. Load DA
if (COMPLETE_SUCCESS != (returnValue = ::gps_DA_LoadByFilepath(m_hBromdll, m_DAHandle, pszFullPath))) {
return returnValue;
}
//- 3. Set up relative info
UINT nStartAddr = 0x00100800;
if (COMPLETE_SUCCESS != (returnValue = ::gps_DA_SetStartAddr(m_hBromdll, m_DAHandle, nStartAddr))) {
return returnValue;
}
return COMPLETE_SUCCESS;
}//END_OF fnLoadDA()
WRAPPER_RETURN CBromDllWrapper::fnLoadImageByScatter(const char * pszImageFullPath, const char * pszScatterFullPath)
{
if( !m_hBromdll ){
return ERROR_DLL_NOT_LOAD;
}
WRAPPER_RETURN returnValue;
//- 1. Create Image Object
if (COMPLETE_SUCCESS != (returnValue = ::gps_DL_Create(m_hBromdll, m_DLHandle))) {
return returnValue;
}
if (NULL == m_DLHandle) {
//Should have returned above
return ERROR_FUNC_INTERNAL_LOGIC;
}
//- 2. Load Scatter file
if (COMPLETE_SUCCESS != (returnValue = this->fnSetByScatter(pszImageFullPath))) {
return returnValue;
}
//- 3. Load Image file
if (COMPLETE_SUCCESS != (returnValue = this->fnLoadImage(pszScatterFullPath))) {
return returnValue;
}
return COMPLETE_SUCCESS;
}//END_OF fnLoadImageByScatter()
WRAPPER_RETURN CBromDllWrapper::fnLoadImage(const char * pszFullPath)
{
if( !m_hBromdll ){
return ERROR_DLL_NOT_LOAD;
}
WRAPPER_RETURN returnValue;
//- 1. Load DL Image
if (COMPLETE_SUCCESS != (returnValue = ::gps_DL_Rom_LoadByFilepath(m_hBromdll, m_DLHandle, pszFullPath))) {
return returnValue;
}
return COMPLETE_SUCCESS;
}//END_OF fnLoadImage()
WRAPPER_RETURN CBromDllWrapper::fnSetByScatter(const char * pszFullPath)
{
if( !m_hBromdll ){
return ERROR_DLL_NOT_LOAD;
}
WRAPPER_RETURN returnValue;
//- 1. ImageObject must be created first
if (NULL == m_DLHandle) {
return ERROR_SOMEWHERE_NONEXPECTED; //This function shouldn't be called! Check logic.
}
//- 2. Load scatter file
if (COMPLETE_SUCCESS != (returnValue = ::gps_DL_LoadScatter(m_hBromdll, m_DLHandle, pszFullPath))) {
return returnValue;
}
//- 3. Get ROM number
UINT nCount = 0;
if (COMPLETE_SUCCESS != (returnValue = ::gps_DL_GetCount(m_hBromdll, m_DLHandle, nCount))) {
return returnValue;
}
if (nCount <= 0) {
return ERROR_SCAT_INCORRECT_FORMAT;
}
//- 4. Setup ROM information
for (UINT i = 0; i < nCount; i++)
{
if (COMPLETE_SUCCESS != (returnValue = ::gps_DL_Rom_SetEnableAttr(m_hBromdll, m_DLHandle, i, TRUE))) {
return returnValue;
}
}
return COMPLETE_SUCCESS;
}//END_OF fnLoadImage()
WRAPPER_RETURN CBromDllWrapper::fnDownloadFirmware(unsigned int szCOM, LPCallBackFlashtool pcbFlashtool, LPCallBackDownload pcbDownload, int * pStopflag)
{
if( !m_hBromdll ){
return ERROR_DLL_NOT_LOAD;
}
if (! pcbFlashtool || ! pcbDownload) {
return ERROR_WRONG_PARAM;
}
m_pCbFlashTool = pcbFlashtool;
m_pCbDownload = pcbDownload;
//-- 0. Update package Length (DL, RB only)
const UINT nPacketLength = 1024;
if (COMPLETE_SUCCESS != this->fnSetDLPacketLength(nPacketLength)){
return ERROR_FAIL_TO_SETPACKETLENGTH;
}
//-- 1. Handle parameter
//-- (COM Port)
COM_PORT_SETTING comPortSetting;
if (COMPLETE_SUCCESS != this->fnArgCOMPortConnect(comPortSetting, szCOM)){
return ERROR_NOT_DECIDE;
}
//-- (FLAHSTOOL_ARG)
GPS_FLASHTOOL_ARG argFlashTool;
memset(&argFlashTool, 0, sizeof(argFlashTool));
if (COMPLETE_SUCCESS != this->fnArgFlashtool(argFlashTool)){
return ERROR_NOT_DECIDE;
}
//-- (FLAHSTOOL_DOWNLOAD_ARG)
GPS_FLASHTOOL_DOWNLOAD_ARG argDownload;
memset(&argDownload, 0, sizeof(argDownload));
if (COMPLETE_SUCCESS != this->fnArgDownload(argDownload)){
return ERROR_NOT_DECIDE;
}
//-- 2. Download and check stop flag
*pStopflag = 0;
GPS_DA_REPORT_T daReport;
WRAPPER_RETURN returnValue;
if (COMPLETE_SUCCESS != (returnValue = ::gps_FlashDownload(m_hBromdll, comPortSetting, argFlashTool, argDownload, pStopflag, daReport))) {
return returnValue;
}
if (BOOT_STOP == *pStopflag ) {
return ERROR_BOOT_STOP; //why?
}
return COMPLETE_SUCCESS;
}//END_OF fnDownloadFirmware()
//------------------------------------------------------------------------------
WRAPPER_RETURN CBromDllWrapper::fnSetRBList(const char * pszRBFullPath, unsigned int szRBAddr, unsigned int szRBLen)
{
if( !m_hBromdll ){
return ERROR_DLL_NOT_LOAD;
}
WRAPPER_RETURN returnValue;
//- 1. Create Image Object
if (COMPLETE_SUCCESS != (returnValue = ::gps_RB_Create(m_hBromdll, m_RBHandle))) {
return returnValue;
}
if (NULL == m_RBHandle) {
//Should have returned above
return ERROR_FUNC_INTERNAL_LOGIC;
}
//- 2. Setup RB file
if (COMPLETE_SUCCESS != (returnValue = this->fnSetRB(pszRBFullPath,szRBAddr,szRBLen))) {
return returnValue;
}
//- 3. Get RB number
UINT nCount = 0;
if (COMPLETE_SUCCESS != (returnValue = ::gps_RB_GetCount(m_hBromdll, m_RBHandle, nCount))) {
return returnValue;
}
if (nCount <= 0) {
return ERROR_RB_GET_COUNT;
}
//- 4. Setup RB information
for (UINT i = 0; i < nCount; i++)
{
if (COMPLETE_SUCCESS != (returnValue = ::gps_RB_SetEnableAttr(m_hBromdll, m_RBHandle, i, TRUE))) {
return returnValue;
}
}
return COMPLETE_SUCCESS;
}//END_OF fnSetRBList()
WRAPPER_RETURN CBromDllWrapper::fnSetRB(const char * pszRBFullPath, unsigned int szRBAddr, unsigned int szRBLen)
{
if( !m_hBromdll ){
return ERROR_DLL_NOT_LOAD;
}
WRAPPER_RETURN returnValue;
//- 1. Setup RB file
if (COMPLETE_SUCCESS != (returnValue = ::gps_RB_Append(m_hBromdll, m_RBHandle, pszRBFullPath,szRBAddr,szRBLen))) {
return returnValue;
}
return COMPLETE_SUCCESS;
}//END_OF fnSetRB()
WRAPPER_RETURN CBromDllWrapper::fnReadbackFirmware(unsigned int szCOM, LPCallBackFlashtool pcbFlashtool, LPCallBackReadback pcbReadback, int * pStopflag)
{
if( !m_hBromdll ){
return ERROR_DLL_NOT_LOAD;
}
if (! pcbFlashtool || ! pcbReadback) {
return ERROR_WRONG_PARAM;
}
m_pCbFlashTool = pcbFlashtool;
m_pCbReadback = pcbReadback;
//-- 0. Update package Length (DL, RB only)
const UINT nPacketLength = 1024;
if (COMPLETE_SUCCESS != this->fnSetRBPacketLength(nPacketLength)){
return ERROR_FAIL_TO_SETPACKETLENGTH;
}
//-- 1. Handle parameter
//-- (COM Port)
COM_PORT_SETTING comPortSetting;
if (COMPLETE_SUCCESS != this->fnArgCOMPortConnect(comPortSetting, szCOM)){
return ERROR_NOT_DECIDE;
}
//-- (FLAHSTOOL_ARG)
GPS_FLASHTOOL_ARG argFlashTool;
memset(&argFlashTool, 0, sizeof(argFlashTool));
if (COMPLETE_SUCCESS != this->fnArgFlashtool(argFlashTool)){
return ERROR_NOT_DECIDE;
}
//-- (FLASHTOOL_READBACK_ARG)
GPS_FLASHTOOL_READBACK_ARG argReadback;
memset(&argReadback, 0, sizeof(argReadback));
if (COMPLETE_SUCCESS != this->fnArgReadback(argReadback)){
return ERROR_NOT_DECIDE;
}
//-- 2. Readback and check stop flag
*pStopflag = 0;
GPS_DA_REPORT_T daReport;
WRAPPER_RETURN returnValue;
if (COMPLETE_SUCCESS != (returnValue = ::gps_FlashReadback(m_hBromdll, comPortSetting, argFlashTool, argReadback, pStopflag, daReport))) {
return returnValue;
}
if (BOOT_STOP == *pStopflag ) {
return ERROR_BOOT_STOP; //why?
}
return COMPLETE_SUCCESS;
}//END_OF fnReadbackFirmware()
//------------------------------------------------------------------------------
WRAPPER_RETURN CBromDllWrapper::fnSetDLPacketLength(UINT nPacketLength)
{
if( !m_hBromdll ){
return ERROR_DLL_NOT_LOAD;
}
WRAPPER_RETURN returnValue;
//- 1. Setup DL Packet Length
if (COMPLETE_SUCCESS != (returnValue = ::gps_DL_SetPacketLength(m_hBromdll, m_DLHandle, nPacketLength))) {
return returnValue;
}
return COMPLETE_SUCCESS;
}//END_OF fnSetDLPacketLength()
WRAPPER_RETURN CBromDllWrapper::fnSetRBPacketLength(UINT nPacketLength)
{
if( !m_hBromdll ){
return ERROR_DLL_NOT_LOAD;
}
WRAPPER_RETURN returnValue;
//- 1. Setup RB Packet Length
if (COMPLETE_SUCCESS != (returnValue = ::gps_RB_SetPacketLength(m_hBromdll, m_RBHandle, nPacketLength))) {
return returnValue;
}
return COMPLETE_SUCCESS;
}//END_OF fnSetRBPacketLength()
//------------------------------------------------------------------------------
WRAPPER_RETURN CBromDllWrapper::fnArgCOMPortConnect(COM_PORT_SETTING& comPortSetting, unsigned int szCOM)
{
if( !m_hBromdll ){
return ERROR_DLL_NOT_LOAD;
}
comPortSetting.com.number = szCOM; //2;//"COM1:";
//comPortSetting.com.handle = xx;
comPortSetting.baudrate = UART_BAUD_115200; // GPS Module device in Phone using 115200 baudrate
//comPortSetting.baudrate = UART_BAUD_9600; //BBK WinCE42 device using 9600 baudrate
comPortSetting.ms_read_timeout = COM_DEFAULT_TIMEOUT; //flashtool.h
comPortSetting.ms_write_timeout = COM_DEFAULT_TIMEOUT;
return COMPLETE_SUCCESS;
}//END_OF fnArgCOMPortConnect()
WRAPPER_RETURN CBromDllWrapper::fnArgFlashtool(GPS_FLASHTOOL_ARG& argFlashtool)
{
if( !m_hBromdll ){
return ERROR_DLL_NOT_LOAD;
}
argFlashtool.bbchip = MT3301; //mtk_mcu.h
argFlashtool.ext_clock = EXT_26M; //mtk_mcu.h
argFlashtool.p_bank0_mem_cfg = NULL; //&g_EMI_Config; //0x00004102
argFlashtool.p_bank1_mem_cfg = NULL; //&g_EMI_Config;
argFlashtool.da_handle = m_DAHandle;
argFlashtool.cb_download_da_init = m_pCbFlashTool->cb_download_da_init;
argFlashtool.cb_download_da_init_arg = m_pCbFlashTool->cb_download_da_init_arg;
argFlashtool.cb_download_da = m_pCbFlashTool->cb_download_da;
argFlashtool.cb_download_da_arg = m_pCbFlashTool->cb_download_da_arg;
argFlashtool.cb_before_process = NULL; //m_pCbFlashTool->cb_before_process;
argFlashtool.cb_before_process_arg = this;
argFlashtool.cb_set_high_speed_baudrate = NULL;
argFlashtool.cb_set_high_speed_baudrate_arg = NULL;
argFlashtool.ms_boot_timeout = BOOT_INFINITE;
argFlashtool.max_start_cmd_retry_count = DEFAULT_BROM_START_CMD_RETRY_COUNT; //1
argFlashtool.cb_in_brom_stage = NULL; //m_pCbFlashTool->cb_in_brom_stage;
argFlashtool.cb_in_brom_stage_arg = &(argFlashtool.bbchip);
argFlashtool.m_baudrate_full_sync_count = 1; //Why this value?
argFlashtool.m_speedup_brom_baudrate = _TRUE;
return COMPLETE_SUCCESS;
}//END_OF fnArgFlashtool()
WRAPPER_RETURN CBromDllWrapper::fnArgDownload(GPS_FLASHTOOL_DOWNLOAD_ARG& argDownload)
{
if( !m_hBromdll ){
return ERROR_DLL_NOT_LOAD;
}
argDownload.dl_handle = m_DLHandle;
argDownload.download_accuracy = ACCURACY_AUTO;
argDownload.cb_download_flash_init = m_pCbDownload->cb_download_flash_init;
argDownload.cb_download_flash_init_arg = m_pCbDownload->cb_download_flash_init_arg;//this;
argDownload.cb_download_flash = m_pCbDownload->cb_download_flash;
argDownload.cb_download_flash_arg = m_pCbDownload->cb_download_flash_arg; //this;
//argDownload.cb_format_report_init = m_pCbDownload->cb_format_report_init; //No format
//argDownload.cb_format_report_init_arg = this;
//argDownload.cb_format_report = m_pCbDownload->cb_format_report;
//argDownload.cb_format_report_arg = this;
argDownload.enable_tgt_res_layout_check = _TRUE;
argDownload.enable_bbchip_eco_check = _TRUE;
return COMPLETE_SUCCESS;
}//END_OF fnArgDownload()
WRAPPER_RETURN CBromDllWrapper::fnArgReadback(GPS_FLASHTOOL_READBACK_ARG& argReadback)
{
if( !m_hBromdll ){
return ERROR_DLL_NOT_LOAD;
}
argReadback.rb_handle = m_RBHandle;
argReadback.readback_accuracy = ACCURACY_AUTO;
argReadback.cb_readback_flash_init = m_pCbReadback->cb_readback_flash_init;
argReadback.cb_readback_flash_init_arg = m_pCbReadback->cb_readback_flash_init_arg;
argReadback.cb_readback_flash = m_pCbReadback->cb_readback_flash;
argReadback.cb_readback_flash_arg = m_pCbReadback->cb_readback_flash_arg;
// argReadback.rb_info =;
return COMPLETE_SUCCESS;
}//END_OF fnArgDownload()
WRAPPER_RETURN CBromDllWrapper::fnDebugOn(const char * pszPath)
{
if( !m_hBromdll ){
return ERROR_DLL_NOT_LOAD;
}
WRAPPER_RETURN returnValue;
//- 1. DebugOn
if (COMPLETE_SUCCESS != (returnValue = ::gps_Brom_DebugOn(m_hBromdll,pszPath))) {
return returnValue;
}
return COMPLETE_SUCCESS;
}//END_OF fnDebugOn()
WRAPPER_RETURN CBromDllWrapper::fnDebugOff(void)
{
if( !m_hBromdll ){
return ERROR_DLL_NOT_LOAD;
}
WRAPPER_RETURN returnValue;
//- 1. DebugOff
if (COMPLETE_SUCCESS != (returnValue = ::gps_Brom_DebugOff(m_hBromdll))) {
return returnValue;
}
return COMPLETE_SUCCESS;
}//END_OF fnDebugOn()