-
Notifications
You must be signed in to change notification settings - Fork 19
/
sb_loader.cpp
587 lines (512 loc) · 14.4 KB
/
sb_loader.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
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
/*
* File: sb_loader.cpp
*
* Copyright (c) 2010-2016 Freescale Semiconductor, Inc. All rights reserved.
* See included license file for license details.
*/
// sb_loader.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "resource.h"
#include <basetyps.h>
#include <setupapi.h>
#include <initguid.h>
#pragma warning( push )
#pragma warning( disable : 4201 )
extern "C" {
#include "hidsdi.h"
}
#pragma warning( pop )
#include "hiddevice.h"
#include "StHidDevice.h"
#include "MxHidDevice.h"
#include "DeviceManager.h"
#include "sb_loader.h"
#include <conio.h>
#include "gitversion.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
DeviceManager* g_pDeviceManager = NULL;
CStHidDevice* g_pHidDevice = NULL;
MxHidDevice* g_pMxHidDevice = NULL;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
// _CrtSetBreakAlloc (161);
CString fwFilename = _T("");
ExtendedFunction function = None;
CString indent = _T("");
int nRetCode = 0;
CFile fwFile;
UCHAR* DataBuf = NULL;
ULONGLONG fwSize = 0;
DeviceType DevType = NoDev;
MxHidDevice::MxFunc MxFunc;
ZeroMemory(&MxFunc, sizeof(MxFunc));
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
//
// Process command line arguements
//
if (ProcessCommandLine(argc, argv, fwFilename, function, &DevType, &MxFunc) == false)
{
nRetCode = 0;
goto ExitMain;
}
//
// Get data
//
if (fwFilename.CompareNoCase(_T("")) != 0)
{
if (fwFile.Open(fwFilename, CFile::modeRead | CFile::shareDenyWrite) == 0)
{
TRACE(__FUNCTION__ " firmware file failed to open.\n");
_tprintf(_T("%sFirmware file %s failed to open.errcode is %d\n"), indent, fwFilename, GetLastError());
nRetCode = 2;
goto ExitMain;
}
fwSize = fwFile.GetLength();
fwSize = (fwSize + ROM_ECC_SIZE_ALIGN - 1) / ROM_ECC_SIZE_ALIGN * ROM_ECC_SIZE_ALIGN;
DataBuf = (UCHAR*)malloc((size_t)fwSize);
fwFile.Read(DataBuf, (UINT)fwSize);
fwFile.Close();
}
//
// Prepare the DeviceManager object for use.
// Note: We must call gDeviceManager::Instance().Close() before the app exits.
g_pDeviceManager = new DeviceManager();
VERIFY(g_pDeviceManager->Open() == ERROR_SUCCESS);
if (!SearchDevice())
goto ExitMain;
switch (g_pHidDevice->GetDevType())
{
case MX23:
case MX28:
case MX8QXPB0:
{
nRetCode = StDownload(fwFilename, DataBuf, fwSize);
}
break;
default:
{
switch (MxFunc.Task)
{
case MxHidDevice::INIT:
if (!g_pMxHidDevice->InitMemoryDevice(MxFunc.MemType))
nRetCode = 5;
TRACE(__FUNCTION__ " MX device initialized.\n");
_tprintf(_T("%sMX device initialized.\n"), indent);
break;
case MxHidDevice::TRANS:
if (!g_pMxHidDevice->Download(DataBuf, fwSize, MxFunc.ImageParameter.PhyRAMAddr4KRL))
{
TRACE(__FUNCTION__ " ERROR: During download.\n");
_tprintf(_T("%s Failed to download %s to the device.\n"), indent, fwFilename);
nRetCode = 3;
return nRetCode;
}
else
{
_tprintf(_T("%sSucceeded to download %s to the device.\n"), indent, fwFilename);
}
break;
case MxHidDevice::EXEC:
if (g_pHidDevice->GetDevType() == MX8QM ||
!g_pMxHidDevice->Execute(MxFunc.ImageParameter.PhyRAMAddr4KRL))
{
TRACE(__FUNCTION__ " ERROR: Exec RAM failed.\n");
_tprintf(_T("% Failed to exec RAM.\n"), indent);
nRetCode = 4;
return nRetCode;
}
TRACE(__FUNCTION__ " Exec RAM successfully.\n");
_tprintf(_T("%sExec RAM successfully.\n"), indent);
break;
case MxHidDevice::JUMP:
if (!g_pMxHidDevice->Jump(MxFunc.ImageParameter.PhyRAMAddr4KRL))
{
TRACE(__FUNCTION__ " ERROR: Jump to RAM failed.\n");
_tprintf(_T("% Failed to jump to RAM.\n"), indent);
nRetCode = 5;
return nRetCode;
}
TRACE(__FUNCTION__ " Jump to RAM successfully.\n");
_tprintf(_T("%sJump to RAM successfully.\n"), indent);
break;
case MxHidDevice::RUN_PLUGIN:
case MxHidDevice::RUN:
nRetCode = MxRun(fwFilename, DataBuf, fwSize, &MxFunc);
break;
case MxHidDevice::READ:
{
UINT value;
if (!g_pMxHidDevice->RegRead(MxFunc.Addr, &value))
{
_tprintf(_T("Read failure\n"));
nRetCode = 5;
return nRetCode;
}
_tprintf(_T("0x%08X: 0x%08x"), MxFunc.Addr, value);
break;
}
case MxHidDevice::WRITE:
if (!g_pMxHidDevice->RegWrite(MxFunc.Addr, MxFunc.Value))
{
_tprintf(_T("Write failure\n"));
nRetCode = 5;
return nRetCode;
}
break;
default:
break;
}
}
break;
}
}
ExitMain:
if (g_pHidDevice)
delete g_pHidDevice;
if (g_pMxHidDevice)
delete g_pMxHidDevice;
if (g_pDeviceManager)
{
g_pDeviceManager->Close();
delete g_pDeviceManager;
}
if (DataBuf)
free(DataBuf);
return nRetCode;
}
BOOL SearchDevice()
{
BOOL bRet = FALSE;
//_tprintf(_T("\nLooking for a Freescale HID device within 2 seconds...\n"));
g_pHidDevice = new CStHidDevice();
bRet = g_pDeviceManager->FindHidDevice(g_pHidDevice, 2);
if (!bRet)
{
TRACE(__FUNCTION__ " HID device not found.\n");
_tprintf(_T("HID device was not found.\n Please place the device in recovery mode and restart the program.\n"));
return FALSE;
}
if ((g_pHidDevice->GetDevType() != MX28) && (g_pHidDevice->GetDevType() != MX23))
{
//g_pHidDevice->Close();
g_pMxHidDevice = new MxHidDevice();
g_pDeviceManager->FindHidDevice(g_pMxHidDevice, 2);
}
TRACE(__FUNCTION__ "Freescale HID device found.\n");
//_tprintf(_T("Found %s.\n"), g_pMxHidDevice->GetUsbDeviceId());
return TRUE;
}
int MxSingleImgRun(CString fwFilename, UCHAR* DataBuf, ULONGLONG fwSize, MxHidDevice::PMxFunc pMxFunc)
{
CString indent = _T("");
int nRetCode = 0;
BOOL bRet = FALSE;
//
// Execute plugin
//
bRet = g_pMxHidDevice->RunPlugIn(DataBuf, fwSize, pMxFunc);
if (!bRet)
{
TRACE(__FUNCTION__ " ERROR: During RunPlugIn.\n");
_tprintf(_T("%s Failed to run plugin %s to the device.\n"), indent, fwFilename);
nRetCode = 3;
return nRetCode;
}
else
{
TRACE(__FUNCTION__ " Executed plugin successfully.\n");
_tprintf(_T("%s\nExecuted plugin successfully.\n"), indent);
}
//
// Load firmware
//
bRet = g_pMxHidDevice->LoadFirmware(DataBuf, fwSize, pMxFunc);
if (!bRet)
{
TRACE(__FUNCTION__ " ERROR: During download.\n");
_tprintf(_T("%s Failed to download %s to the device.\n"), indent, fwFilename);
nRetCode = 3;
return nRetCode;
}
else
{
_tprintf(_T("%sSucceed to download %s to the device.\n"), indent, fwFilename);
}
if (pMxFunc->Task != MxHidDevice::RUN_PLUGIN)
{
if (pMxFunc->pIVT && g_pMxHidDevice->SkipDCD())
{
if (!g_pMxHidDevice->Jump(pMxFunc->pIVT->SelfAddr))
{
TRACE(__FUNCTION__ " ERROR: During RunPlugIn.\n");
_tprintf(_T("%s Failed to run %s to the device.\n"), indent, fwFilename);
nRetCode = 3;
return nRetCode;
}
}
else if (!g_pMxHidDevice->Execute(pMxFunc->ImageParameter.ExecutingAddr))
{
TRACE(__FUNCTION__ " ERROR: During RunPlugIn.\n");
_tprintf(_T("%s Failed to run plugin %s to the device.\n"), indent, fwFilename);
nRetCode = 3;
return nRetCode;
}
TRACE(__FUNCTION__ " Jump to RAM successfully.\n");
_tprintf(_T("%sRun into the image successfully.\n"), indent);
}
return nRetCode;
}
int MxMultiImgRun(UCHAR* DataBuf, ULONGLONG fwSize)
{
int bRet;
bRet = g_pMxHidDevice->RunMxMultiImg(DataBuf, fwSize);
if (!bRet)
{
TRACE(__FUNCTION__ " ERROR: During MxMultiImgRun.\n");
_tprintf(_T(" ERROR: During MxMultiImgRun.\n"));
return -1;
} else {
TRACE(__FUNCTION__ " Ran into image successfully.\n");
_tprintf(_T("%s\nExecuted plugin successfully.\n"));
}
return 0;
}
int MxRun(CString fwFilename, UCHAR* DataBuf, ULONGLONG fwSize, MxHidDevice::PMxFunc pMxFunc)
{
if (g_pHidDevice->GetDevType() < MX8QM)
return MxSingleImgRun(fwFilename, DataBuf, fwSize, pMxFunc);
else {
if (pMxFunc->Task == MxHidDevice::RUN_PLUGIN)
{
TRACE(__FUNCTION__ "RUN_PLUGIN is not supported for this device\n");
_tprintf(_T("RUN_PLUGIN is not supportedfor this device\n"));
return -1;
}
return MxMultiImgRun(DataBuf, fwSize);
}
}
int StDownload(CString fwFilename, UCHAR* DataBuf, ULONGLONG fwSize)
{
CString indent = _T("");
int nRetCode = 0;
BOOL bRet = FALSE;
//
// Load firmware
//
TRACE(__FUNCTION__ " HID device found.\n");
_tprintf(_T("%sDownloading %s to device.\n"), indent, fwFilename);
int error = g_pHidDevice->Download(DataBuf, fwSize, _T(" "));
if (error != ERROR_SUCCESS)
{
TRACE(__FUNCTION__ " ERROR: During download.\n");
_tprintf(_T("%s Error(%d) during download.\n%sQuitting.\n"), indent, error, indent);
nRetCode = error;
return nRetCode;
}
g_pDeviceManager->WaitForChange(DeviceManager::DEVICE_REMOVAL_EVT, 10);
return nRetCode;
}
UINT String2Uint(CString attr)
{
UINT retVal;
if (attr.Left(2) == _T("0x"))
{
TCHAR *p;
retVal = _tcstoul(attr.Mid(2), &p, 16);
}
else
{
retVal = _tstoi(attr);
}
return retVal;
}
BOOL String2Bool(CString attr)
{
if (attr.CompareNoCase(_T("TRUE")) == 0)
return TRUE;
return FALSE;
}
bool ProcessCommandLine(int argc, TCHAR* argv[], CString& fwFilename, ExtendedFunction& function, DeviceType* DevType, MxHidDevice::PMxFunc pMxFunc)
{
bool ret = true;
CString arg;
for (int i = 0; i < argc; ++i)
{
arg = argv[i];
if (arg.CompareNoCase(_T("/tss")) == 0 || arg.CompareNoCase(_T("-tss")) == 0)
{
function = CaptureTssFunction;
}
else if ((arg.CompareNoCase(_T("/f")) == 0 || arg.CompareNoCase(_T("-f")) == 0) && i <= argc - 1)
{
fwFilename = argv[++i];
}
else if (arg.Compare(_T("/?")) == 0 || arg.CompareNoCase(_T("/h")) == 0 || arg.CompareNoCase(_T("-h")) == 0)
{
PrintUsage();
ret = false;
}
else if (arg.CompareNoCase(_T("/v")) == 0 || arg.CompareNoCase(_T("-v")) == 0)
{
_tprintf(_T("Version: %s--%s\nBuild time: %s"), VERSION, _T(GIT_VERSION), _T(__DATE__));
ret = false;
}
else if (arg.CompareNoCase(_T("-init")) == 0 && i <= argc - 1)
{
pMxFunc->Task = MxHidDevice::INIT;
arg = argv[++i];
if (arg.CompareNoCase(_T("LPDDR2_V3")) == 0)
{
pMxFunc->MemType = MxHidDevice::LPDDR2_V3;
}
}
else if (arg.CompareNoCase(_T("-trans")) == 0 && i <= argc - 1)
{
pMxFunc->Task = MxHidDevice::TRANS;
pMxFunc->ImageParameter.PhyRAMAddr4KRL = String2Uint(argv[++i]);
}
else if (arg.CompareNoCase(_T("-exec")) == 0 && i <= argc - 1)
{
pMxFunc->Task = MxHidDevice::EXEC;
pMxFunc->ImageParameter.PhyRAMAddr4KRL = String2Uint(argv[++i]);
}
else if (arg.CompareNoCase(_T("-jump")) == 0 && i <= argc - 1)
{
pMxFunc->Task = MxHidDevice::JUMP;
pMxFunc->ImageParameter.PhyRAMAddr4KRL = String2Uint(argv[++i]);
}
else if (arg.CompareNoCase(_T("-nojump")) == 0 && i <= argc - 1)
{
pMxFunc->Task = MxHidDevice::RUN_PLUGIN;
}
else if (arg.CompareNoCase(_T("-rd")) == 0 && i <= argc - 1)
{
pMxFunc->Task = MxHidDevice::READ;
pMxFunc->Addr = String2Uint(argv[++i]);
return true;
}
else if (arg.CompareNoCase(_T("-wr")) == 0 && i <= argc - 1)
{
pMxFunc->Task = MxHidDevice::WRITE;
pMxFunc->Addr = String2Uint(argv[++i]);
pMxFunc->Value = String2Uint(argv[++i]);
return true;
}
else if (1 < argc)
{
pMxFunc->Task = MxHidDevice::RUN;
}
else
{
PrintUsage();
ret = false;
}
}
fwFilename.Trim();
if ((pMxFunc->Task != MxHidDevice::EXEC) &&
fwFilename.IsEmpty())
{
PrintUsage();
ret = false;
}
return ret;
}
void PrintUsage()
{
CString usage =
_T("\nUsage: sb_loader.exe [/f <filename>] [/tss] [/? | /h]\n\n\n") \
_T("\t-f or /f <filename> - where <filename> is the file to download. Default: \"firmware.sb\"\n\n") \
_T("\t-tss or /tss - captures and prints the TSS output after the file is downloaded.\n\n") \
_T("\t-h or /h - displays this screen.\n\n") \
_T("\t-nojump - Load but don't execute the image in which plugin is contained to RAM. Available for mx50 and later.\n\n") \
_T("\t-trans - Load the image to RAM, the target address must be followed. Available for mx50 and later.\n\n") \
_T("\t-exec - Execute the image, the execution address must be followed. Available for mx50 and later.\n\n") \
_T("\t-jump - Jump to the image, the image address must be followed. Available for mx50 and later.\n\n") \
_T("\t-init - initialize RAM by using the settings defined in memoryinit.h. Available for mx50 and later.\n\n") \
_T("\tbelow instance download and run an image:\n") \
_T("\tsb_loader -f uboot.bin\n\n");
_tprintf(usage);
}
#define SIZE_OF_BUFFER 1024
int CaptureTss(CString indent)
{
USES_CONVERSION_EX;
int error = ERROR_SUCCESS;
TRACE(__FUNCTION__ " Capturing TSS output.\n");
_tprintf(_T("%sCapturing TSS output. Hit 'q' to exit.\n"), indent);
indent += _T(" ");
//
// Find device in HID mode
//
g_pDeviceManager->FindHidDevice(g_pHidDevice, 10);
if (g_pHidDevice == NULL)
{
error = ERROR_BAD_UNIT;
TRACE(__FUNCTION__ " Never found HID device.\n");
_tprintf(_T("%sNever found HID device.\n%sQuitting\n\n"), indent, indent);
return error;
}
TRACE(__FUNCTION__ " HID device found.\n");
_tprintf(_T("%sFound %s.\n\n"), indent, g_pHidDevice->GetUsbDeviceId());
//
// Read the data.
//
UCHAR u8UsbBuffer[SIZE_OF_BUFFER + 1]; // I increased the size from 32 to 1024 to see is I could avoid heap corruption
UCHAR tempBuff[25];
CString str;
USHORT len = 0;
while (1)
{
// USB Interface
// Read USB Stream, we read one packet at a time
error = g_pHidDevice->Read(u8UsbBuffer, sizeof(_TSS_PACKET));
if (error == ERROR_SUCCESS) {
if (*(USHORT*)&u8UsbBuffer[3] & (1 << 15))
{
len = ((*(USHORT*)&u8UsbBuffer[3] & 0x3fff) % 24) + 1;
}
else
{
len = 24;
}
memset(tempBuff, 0, 25);
memcpy(tempBuff, &u8UsbBuffer[7], len);
str = A2W_EX((LPCSTR)tempBuff, len + 1);
_tprintf(_T("%s"), str);
if (str.Find(_T("RPC Ready:")) != -1)
break;
}
if (g_pDeviceManager->WaitForChange(DeviceManager::DEVICE_REMOVAL_EVT, 0))
{
error = ERROR_INVALID_HANDLE;
TRACE(__FUNCTION__ " Device disconnected while getting info.\n");
_tprintf(_T("\n%sDevice disconnected while getting info.\n%sQuitting\n\n"), indent, indent);
break;
}
// See if user pressed 'q'
if (_kbhit())
{
TCHAR ch = _gettch();
if (ch == _T('q') || ch == _T('Q'))
{
_tprintf(_T("%s\n\nUser hit %c to exit.\n%sQuitting\n\n"), indent, ch, indent);
break;
}
}
}
return error;
}