-
Notifications
You must be signed in to change notification settings - Fork 109
/
RKComm.cpp
793 lines (689 loc) · 18.5 KB
/
RKComm.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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/*
* (C) Copyright 2017 Fuzhou Rockchip Electronics Co., Ltd
* Seth Liu 2017.03.01
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include "RKComm.h"
#include "RKLog.h"
#include "Endian.h"
extern unsigned short CRC_CCITT(unsigned char* p, UINT CalculateNumber);
CRKComm::CRKComm(CRKLog *pLog)
{
memset(&m_deviceDesc,0,sizeof(STRUCT_RKDEVICE_DESC));
m_log = pLog;
}
CRKComm::~CRKComm()
{
}
CRKUsbComm::CRKUsbComm(STRUCT_RKDEVICE_DESC devDesc, CRKLog *pLog, bool &bRet):CRKComm(pLog)
{
bRet = InitializeUsb(devDesc);
}
CRKUsbComm::~CRKUsbComm()
{
UninitializeUsb();
}
bool CRKUsbComm::InitializeUsb(STRUCT_RKDEVICE_DESC devDesc)
{
m_pUsbHandle = NULL;
m_pipeBulkIn = m_pipeBulkOut = 0;
m_interfaceNum = -1;
if (!devDesc.pUsbHandle) {
return false;
}
memcpy(&m_deviceDesc, &devDesc, sizeof(STRUCT_RKDEVICE_DESC));
int iRet;
iRet = libusb_open((libusb_device *)devDesc.pUsbHandle, (libusb_device_handle **)&m_pUsbHandle);
if (iRet!=0) {
if (m_log) {
m_log->Record("Error:InitializeUsb-->open device failed,err=%d", iRet);
}
return false;
}
struct libusb_config_descriptor *pConfigDesc=NULL;
iRet = libusb_get_active_config_descriptor((libusb_device *)devDesc.pUsbHandle, &pConfigDesc);
if (iRet!=0) {
if (m_log) {
m_log->Record("Error:InitializeUsb-->get device config descriptor failed, err=%d", iRet);
}
return false;
}
int i, j, k;
const struct libusb_interface *pInterface;
const struct libusb_endpoint_descriptor *pEndpointDesc;
const struct libusb_interface_descriptor *pInterfaceDesc;
for(i = 0; i < pConfigDesc->bNumInterfaces; i++) {
pInterface = pConfigDesc->interface + i;
for(j = 0; j < pInterface->num_altsetting; j++) {
pInterfaceDesc = pInterface->altsetting+j;
if (m_deviceDesc.emUsbType == RKUSB_MSC) {
if( (pInterfaceDesc->bInterfaceClass != 8) || (pInterfaceDesc->bInterfaceSubClass != 6) || (pInterfaceDesc->bInterfaceProtocol != 0x50))
continue;
}
else
{
if( (pInterfaceDesc->bInterfaceClass != 0xff) || (pInterfaceDesc->bInterfaceSubClass != 6) || (pInterfaceDesc->bInterfaceProtocol != 5))
continue;
}
for(k = 0; k < pInterfaceDesc->bNumEndpoints; k++) {
pEndpointDesc = pInterfaceDesc->endpoint+k;
if ((pEndpointDesc->bEndpointAddress & 0x80) == 0) {
if (m_pipeBulkOut == 0)
m_pipeBulkOut = pEndpointDesc->bEndpointAddress;
}
else {
if (m_pipeBulkIn == 0)
m_pipeBulkIn = pEndpointDesc->bEndpointAddress;
}
if ((m_pipeBulkIn != 0) && (m_pipeBulkOut != 0)) {//found it
m_interfaceNum = i;
libusb_free_config_descriptor(pConfigDesc);
iRet = libusb_claim_interface((libusb_device_handle *)m_pUsbHandle, m_interfaceNum);
if (iRet != 0) {
if (m_log) {
m_log->Record("Error:libusb_claim_interface failed,err=%d", iRet);
}
return false;
}
return true;
}
}
}
}
libusb_free_config_descriptor(pConfigDesc);
return false;
}
void CRKUsbComm::UninitializeUsb()
{
if (m_pUsbHandle) {
libusb_close((libusb_device_handle *)m_pUsbHandle);
m_pUsbHandle = NULL;
}
memset(&m_deviceDesc, 0, sizeof(STRUCT_RKDEVICE_DESC));
m_pipeBulkIn = m_pipeBulkOut = 0;
return ;
}
bool CRKUsbComm::Reset_Usb_Config(STRUCT_RKDEVICE_DESC devDesc)
{
bool bRet;
UninitializeUsb();
bRet = InitializeUsb(devDesc);
return bRet;
}
bool CRKUsbComm::Reset_Usb_Device()
{
int iRet = -1;
if (m_pUsbHandle) {
iRet=libusb_reset_device((libusb_device_handle *)m_pUsbHandle);
}
return (iRet == 0) ? true : false;
}
bool CRKUsbComm::RKU_Read(BYTE* lpBuffer, DWORD dwSize)
{
int iRet;
int nRead;
iRet = libusb_bulk_transfer((libusb_device_handle *)m_pUsbHandle, m_pipeBulkIn, lpBuffer, dwSize, &nRead, CMD_TIMEOUT);
if (iRet!=0) {
if (m_log) {
m_log->Record("Error:RKU_Read failed,err=%d", iRet);
}
return false;
}
if (nRead != (int)dwSize) {
if (m_log) {
m_log->Record("Error:RKU_Read failed, size=%d, read=%d", dwSize, nRead);
}
return false;
}
return true;
}
bool CRKUsbComm::RKU_Write(BYTE* lpBuffer, DWORD dwSize)
{
int iRet;
int nWrite;
iRet = libusb_bulk_transfer((libusb_device_handle *)m_pUsbHandle, m_pipeBulkOut, lpBuffer, dwSize, &nWrite, CMD_TIMEOUT);
if (iRet != 0) {
if (m_log) {
m_log->Record("Error:RKU_Write failed, err=%d", iRet);
}
return false;
}
if (nWrite != (int)dwSize) {
if (m_log) {
m_log->Record("Error:RKU_Write failed, size=%d, read=%d", dwSize, nWrite);
}
return false;
}
return true;
}
int CRKUsbComm::RandomInteger(int low, int high)
{
int k;
double d;
d = (double)rand() / ((double)RAND_MAX + 1);
k = (int)(d * (high - low + 1));
return (low + k);
}
DWORD CRKUsbComm::MakeCBWTag()
{
DWORD tag = 0;
int i = 0;
for(i=0; i<4; i++){
tag <<= 8;
tag += RandomInteger(0, 0xFF);
}
return tag;
}
void CRKUsbComm::InitializeCBW(PCBW pCBW, USB_OPERATION_CODE code)
{
memset(pCBW,0, sizeof(CBW));
pCBW->dwCBWSignature = CBW_SIGN;
pCBW->dwCBWTag = MakeCBWTag();
pCBW->cbwcb.ucOperCode = code;
switch(code) {
case TEST_UNIT_READY: /* Test Unit Ready : 0 */
case READ_FLASH_ID: /* Read Flash ID : 1 */
case READ_FLASH_INFO:
case READ_CHIP_INFO:
case READ_EFUSE:
case READ_CAPABILITY:
pCBW->ucCBWFlags= DIRECTION_IN;
pCBW->ucCBWCBLength = 0x06;
break;
case DEVICE_RESET: /* Reset Device : 0xff */
case ERASE_SYSTEMDISK:
case SET_RESET_FLAG:
pCBW->ucCBWFlags = DIRECTION_OUT;
pCBW->ucCBWCBLength = 0x06;
break;
case TEST_BAD_BLOCK: /* Test Bad Block : 3 */
case READ_SECTOR: /* Read Pages : 4 */
case READ_LBA: /* Read Pages : 4 */
case READ_SDRAM: /* Write Pages : 15 */
case READ_SPI_FLASH:
case READ_NEW_EFUSE:
pCBW->ucCBWFlags = DIRECTION_IN;
pCBW->ucCBWCBLength = 0x0a;
break;
case WRITE_SECTOR: /* Write Pages : 5 */
case WRITE_LBA: /* Write Pages : 15 */
case WRITE_SDRAM: /* Write Pages : 15 */
case EXECUTE_SDRAM:
case ERASE_NORMAL: /* Erase Blocks : 6 */
case ERASE_FORCE: /* Read Spare : 11 */
case WRITE_EFUSE:
case WRITE_SPI_FLASH:
case WRITE_NEW_EFUSE:
case ERASE_LBA:
pCBW->ucCBWFlags = DIRECTION_OUT;
pCBW->ucCBWCBLength = 0x0a;
break;
default:
break;
}
}
bool CRKUsbComm::RKU_ClearBuffer(CBW& cbw, CSW& csw)
{
DWORD dwReadBytes=0;
DWORD dwTotalRead=0;
int iTryCount;
iTryCount = 3;
do {
dwReadBytes = RKU_Read_EX((BYTE*)&csw, sizeof(CSW) );
if (UFI_CHECK_SIGN(cbw,csw))
{
return true;
}
if (dwReadBytes != sizeof(CSW))
{
iTryCount--;
sleep(3);
}
dwTotalRead += dwReadBytes;
if (dwTotalRead >= MAX_CLEAR_LEN)
{
break;
}
}while ( iTryCount > 0 );
return false;
}
DWORD CRKUsbComm::RKU_Read_EX(BYTE* lpBuffer, DWORD dwSize)
{
int iRet;
int nRead;
iRet = libusb_bulk_transfer((libusb_device_handle *)m_pUsbHandle, m_pipeBulkIn, lpBuffer, dwSize, &nRead, 5000);
if (iRet != 0) {
if (m_log) {
m_log->Record("Error:RKU_Read_EX failed, err=%d", iRet);
}
return 0;
}
return nRead;
}
int CRKUsbComm::RKU_EraseBlock(BYTE ucFlashCS, DWORD dwPos, DWORD dwCount, BYTE ucEraseType)
{
if ((m_deviceDesc.emUsbType != RKUSB_LOADER) && (m_deviceDesc.emUsbType != RKUSB_MASKROM)) {
if (m_log) {
m_log->Record("Error:RKU_EraseBlock failed,device not support");
}
return ERR_DEVICE_NOT_SUPPORT;
}
CBW cbw;
CSW csw;
USHORT usCount;
usCount = dwCount;
if(dwCount > MAX_ERASE_BLOCKS)
return ERR_CROSS_BORDER;
InitializeCBW(&cbw, (USB_OPERATION_CODE)ucEraseType);
cbw.ucCBWLUN = ucFlashCS;
cbw.cbwcb.dwAddress = EndianU32_LtoB(dwPos);
cbw.cbwcb.usLength = EndianU16_LtoB(usCount);
if(!RKU_Write((BYTE *)&cbw, sizeof(CBW)))
{
return ERR_DEVICE_WRITE_FAILED;
}
if(!RKU_Read((BYTE *)&csw, sizeof(CSW)))
{
return ERR_DEVICE_READ_FAILED;
}
if( !UFI_CHECK_SIGN(cbw, csw) )
return ERR_CMD_NOTMATCH;
if(csw.ucCSWStatus == 1)
return ERR_FOUND_BAD_BLOCK;
return ERR_SUCCESS;
}
int CRKUsbComm::RKU_ReadChipInfo(BYTE* lpBuffer)
{
if ((m_deviceDesc.emUsbType != RKUSB_LOADER) && (m_deviceDesc.emUsbType != RKUSB_MASKROM)) {
if (m_log) {
m_log->Record("Error:RKU_ReadChipInfo failed,device not support");
}
return ERR_DEVICE_NOT_SUPPORT;
}
CBW cbw;
CSW csw;
InitializeCBW(&cbw, READ_CHIP_INFO);
cbw.dwCBWTransferLength = 16;
if(!RKU_Write((BYTE *)&cbw, sizeof(CBW)))
{
return ERR_DEVICE_WRITE_FAILED;
}
if(!RKU_Read(lpBuffer, 16))
{
return ERR_DEVICE_READ_FAILED;
}
if(!RKU_Read( (BYTE *)&csw, sizeof(CSW)))
{
return ERR_DEVICE_READ_FAILED;
}
if( !UFI_CHECK_SIGN(cbw, csw) )
return ERR_CMD_NOTMATCH;
return ERR_SUCCESS;
}
int CRKUsbComm::RKU_ReadFlashID(BYTE* lpBuffer)
{
if ((m_deviceDesc.emUsbType != RKUSB_LOADER) && (m_deviceDesc.emUsbType != RKUSB_MASKROM)) {
if (m_log) {
m_log->Record("Error:RKU_ReadChipInfo failed,device not support");
}
return ERR_DEVICE_NOT_SUPPORT;
}
CBW cbw;
CSW csw;
InitializeCBW(&cbw, READ_FLASH_ID);
cbw.dwCBWTransferLength = 5;
if(!RKU_Write((BYTE *)&cbw, sizeof(CBW)))
{
return ERR_DEVICE_WRITE_FAILED;
}
if(!RKU_Read(lpBuffer, 5))
{
return ERR_DEVICE_READ_FAILED;
}
if(!RKU_Read( (BYTE *)&csw, sizeof(CSW)))
{
return ERR_DEVICE_READ_FAILED;
}
if( !UFI_CHECK_SIGN(cbw, csw) )
return ERR_CMD_NOTMATCH;
return ERR_SUCCESS;
}
int CRKUsbComm::RKU_ReadFlashInfo(BYTE* lpBuffer, UINT *puiRead)
{
if ((m_deviceDesc.emUsbType != RKUSB_LOADER) && (m_deviceDesc.emUsbType != RKUSB_MASKROM)) {
if (m_log) {
m_log->Record("Error:RKU_ReadFlashInfo failed,device not support");
}
return ERR_DEVICE_NOT_SUPPORT;
}
CBW cbw;
CSW csw;
InitializeCBW(&cbw, READ_FLASH_INFO);
cbw.dwCBWTransferLength = 11;
if(!RKU_Write((BYTE *)&cbw, sizeof(CBW)))
{
return ERR_DEVICE_WRITE_FAILED;
}
DWORD dwRead;
dwRead = RKU_Read_EX(lpBuffer, 512);
if ((dwRead < 11) || (dwRead > 512))
{
return ERR_DEVICE_READ_FAILED;
}
if (puiRead)
{
*puiRead = dwRead;
}
/*
if(!RKU_Read(hDev, lpBuffer, 11))
{
return ERR_DEVICE_READ_FAILED;
}
*/
if(!RKU_Read((BYTE *)&csw, sizeof(CSW)))
{
return ERR_DEVICE_READ_FAILED;
}
if( !UFI_CHECK_SIGN(cbw, csw) )
return ERR_CMD_NOTMATCH;
return ERR_SUCCESS;
}
int CRKUsbComm::RKU_ReadCapability(BYTE* lpBuffer)
{
if ((m_deviceDesc.emUsbType != RKUSB_LOADER) && (m_deviceDesc.emUsbType != RKUSB_MASKROM)) {
if (m_log) {
m_log->Record("Error:RKU_ReadCapability failed,device not support");
}
return ERR_DEVICE_NOT_SUPPORT;
}
CBW cbw;
CSW csw;
DWORD dwRead;
InitializeCBW(&cbw, READ_CAPABILITY);
cbw.dwCBWTransferLength = 8;
if(!RKU_Write((BYTE*)&cbw, sizeof(CBW)))
{
return ERR_DEVICE_WRITE_FAILED;
}
dwRead = RKU_Read_EX((BYTE*)&csw, sizeof(CSW));
if(dwRead != 8)
{
return ERR_DEVICE_READ_FAILED;
}
memcpy(lpBuffer, (BYTE*)&csw, 8);
if(!RKU_Read((BYTE*)&csw, sizeof(CSW)))
{
return ERR_DEVICE_READ_FAILED;
}
if( !UFI_CHECK_SIGN(cbw, csw) )
return ERR_CMD_NOTMATCH;
return ERR_SUCCESS;
}
int CRKUsbComm::RKU_ReadLBA(DWORD dwPos, DWORD dwCount, BYTE* lpBuffer, BYTE bySubCode)
{
if ((m_deviceDesc.emUsbType != RKUSB_LOADER) && (m_deviceDesc.emUsbType != RKUSB_MASKROM)) {
if (m_log) {
m_log->Record("Error:RKU_ReadLBA failed,device not support");
}
return ERR_DEVICE_NOT_SUPPORT;
}
CBW cbw;
CSW csw;
USHORT wSectorSize;
USHORT usSectorLen;
wSectorSize = 512;
usSectorLen=dwCount;
InitializeCBW(&cbw, READ_LBA);
cbw.dwCBWTransferLength = dwCount * wSectorSize;
cbw.cbwcb.dwAddress = EndianU32_LtoB(dwPos);
cbw.cbwcb.usLength = EndianU16_LtoB(usSectorLen);
cbw.cbwcb.ucReserved = bySubCode;
if(!RKU_Write((BYTE *)&cbw, sizeof(CBW)))
{
return ERR_DEVICE_WRITE_FAILED;
}
DWORD dwTotal;
dwTotal = usSectorLen * wSectorSize;
if(!RKU_Read(lpBuffer, dwTotal))
{
return ERR_DEVICE_READ_FAILED;
}
if(!RKU_Read((BYTE*)&csw, sizeof(CSW)))
{
return ERR_DEVICE_READ_FAILED;
}
if( !UFI_CHECK_SIGN(cbw, csw) )
return ERR_CMD_NOTMATCH;
if(csw.ucCSWStatus == 1)
return ERR_FAILED;
return ERR_SUCCESS;
}
int CRKUsbComm::RKU_ResetDevice(BYTE bySubCode)
{
if ((m_deviceDesc.emUsbType != RKUSB_LOADER) && (m_deviceDesc.emUsbType != RKUSB_MASKROM)) {
if (m_log) {
m_log->Record("Error:RKU_ResetDevice failed,device not support");
}
return ERR_DEVICE_NOT_SUPPORT;
}
CBW cbw;
CSW csw;
InitializeCBW(&cbw, DEVICE_RESET);
cbw.cbwcb.ucReserved = bySubCode;
if(!RKU_Write((BYTE *)&cbw, sizeof(CBW)))
{
return ERR_DEVICE_WRITE_FAILED;
}
if(!RKU_Read((BYTE *)&csw, sizeof(CSW)))
{
return ERR_DEVICE_READ_FAILED;
}
if( !UFI_CHECK_SIGN(cbw, csw) ) {
bool bRet;
bRet = RKU_ClearBuffer(cbw, csw);
if (!bRet) {
return ERR_CMD_NOTMATCH;
}
}
if(csw.ucCSWStatus == 1)
return ERR_FAILED;
return ERR_SUCCESS;
}
int CRKUsbComm::RKU_TestDeviceReady(DWORD *dwTotal, DWORD *dwCurrent, BYTE bySubCode)
{
if ((m_deviceDesc.emUsbType != RKUSB_LOADER) && (m_deviceDesc.emUsbType != RKUSB_MASKROM)) {
if (m_log) {
m_log->Record("Error:RKU_TestDeviceReady failed,device not support");
}
return ERR_DEVICE_NOT_SUPPORT;
}
CBW cbw;
CSW csw;
InitializeCBW(&cbw, TEST_UNIT_READY);
cbw.cbwcb.ucReserved = bySubCode;
if(!RKU_Write( (BYTE *)&cbw, sizeof(CBW))) {
return ERR_DEVICE_WRITE_FAILED;
}
if(!RKU_Read( (BYTE *)&csw, sizeof(CSW))) {
return ERR_DEVICE_READ_FAILED;
}
if( !UFI_CHECK_SIGN(cbw, csw) ) {
bool bRet;
bRet = RKU_ClearBuffer(cbw ,csw);
if (!bRet) {
return ERR_CMD_NOTMATCH;
}
}
if ((dwTotal!=NULL)&&(dwCurrent!=NULL)) {
*dwCurrent = (csw.dwCBWDataResidue >>16);
*dwTotal = (csw.dwCBWDataResidue & 0x0000FFFF);
*dwTotal = EndianU16_BtoL(*dwTotal);
*dwCurrent = EndianU16_BtoL(*dwCurrent);
}
if(csw.ucCSWStatus == 1) {
return ERR_DEVICE_UNREADY;
}
return ERR_DEVICE_READY;
}
int CRKUsbComm::RKU_WriteLBA(DWORD dwPos, DWORD dwCount, BYTE* lpBuffer, BYTE bySubCode)
{
if ((m_deviceDesc.emUsbType != RKUSB_LOADER) && (m_deviceDesc.emUsbType != RKUSB_MASKROM)) {
if (m_log) {
m_log->Record("Error:RKU_WriteLBA failed,device not support");
}
return ERR_DEVICE_NOT_SUPPORT;
}
CBW cbw;
CSW csw;
USHORT wSectorSize;
USHORT usCount;
wSectorSize = 512;
usCount = dwCount;
DWORD dwTotal = usCount * wSectorSize;
InitializeCBW(&cbw, WRITE_LBA);
cbw.dwCBWTransferLength = dwCount * wSectorSize;
cbw.cbwcb.dwAddress = EndianU32_LtoB(dwPos);
cbw.cbwcb.usLength = EndianU16_LtoB(usCount);
cbw.cbwcb.ucReserved = bySubCode;
if(!RKU_Write( (BYTE *)&cbw, sizeof(CBW))) {
return ERR_DEVICE_WRITE_FAILED;
}
if(!RKU_Write( lpBuffer, dwTotal)) {
return ERR_DEVICE_WRITE_FAILED;
}
if(!RKU_Read( (BYTE *)&csw, sizeof(CSW))) {
return ERR_DEVICE_READ_FAILED;
}
if( !UFI_CHECK_SIGN(cbw, csw) )
return ERR_CMD_NOTMATCH;
if(csw.ucCSWStatus == 1)
return ERR_FAILED;
return ERR_SUCCESS;
}
int CRKUsbComm::RKU_EraseLBA(DWORD dwPos, DWORD dwCount)
{
if ((m_deviceDesc.emUsbType != RKUSB_LOADER) && (m_deviceDesc.emUsbType != RKUSB_MASKROM)) {
if (m_log) {
m_log->Record("Error:RKU_WriteLBA failed,device not support");
}
return ERR_DEVICE_NOT_SUPPORT;
}
CBW cbw;
CSW csw;
USHORT usCount;
usCount = dwCount;
InitializeCBW(&cbw, ERASE_LBA);
cbw.cbwcb.dwAddress = EndianU32_LtoB(dwPos);
cbw.cbwcb.usLength = EndianU16_LtoB(usCount);
if(!RKU_Write( (BYTE *)&cbw, sizeof(CBW))) {
return ERR_DEVICE_WRITE_FAILED;
}
if(!RKU_Read( (BYTE *)&csw, sizeof(CSW))) {
return ERR_DEVICE_READ_FAILED;
}
if( !UFI_CHECK_SIGN(cbw, csw) )
return ERR_CMD_NOTMATCH;
if(csw.ucCSWStatus == 1)
return ERR_FAILED;
return ERR_SUCCESS;
}
int CRKUsbComm::RKU_WriteSector(DWORD dwPos, DWORD dwCount, BYTE *lpBuffer)
{
if ((m_deviceDesc.emUsbType != RKUSB_LOADER) && (m_deviceDesc.emUsbType != RKUSB_MASKROM)) {
if (m_log) {
m_log->Record("Error:RKU_WriteSector failed,device not support");
}
return ERR_DEVICE_NOT_SUPPORT;
}
CBW cbw;
CSW csw;
USHORT wSectorSize;
USHORT usCount;
usCount=dwCount;
if(usCount > 32)
return ERR_CROSS_BORDER;
wSectorSize = 528;
InitializeCBW(&cbw, WRITE_SECTOR);
cbw.dwCBWTransferLength = dwCount * wSectorSize;
cbw.cbwcb.dwAddress = EndianU32_LtoB(dwPos);
cbw.cbwcb.usLength = EndianU16_LtoB(usCount);
if(!RKU_Write( (BYTE *)&cbw, sizeof(CBW))) {
return ERR_DEVICE_WRITE_FAILED;
}
if(!RKU_Write( lpBuffer, usCount * wSectorSize)) {
return ERR_DEVICE_WRITE_FAILED;
}
if(!RKU_Read( (BYTE *)&csw, sizeof(CSW))) {
return ERR_DEVICE_READ_FAILED;
}
if( !UFI_CHECK_SIGN(cbw, csw) )
return ERR_CMD_NOTMATCH;
if(csw.ucCSWStatus == 1)
return ERR_FAILED;
return ERR_SUCCESS;
}
int CRKUsbComm::RKU_DeviceRequest(DWORD dwRequest, BYTE *lpBuffer, DWORD dwDataSize)
{
if (m_deviceDesc.emUsbType != RKUSB_MASKROM) {
if (m_log) {
m_log->Record("Error:RKU_DeviceRequest failed,device not support");
}
return ERR_DEVICE_NOT_SUPPORT;
}
if ((dwRequest != 0x0471) && (dwRequest != 0x0472)) {
if (m_log) {
m_log->Record("Error:RKU_DeviceRequest failed,request not support");
}
return ERR_REQUEST_NOT_SUPPORT;
}
bool bSendPendPacket = false;
USHORT crcValue = 0xffff;
BYTE *pData = NULL;
pData = new BYTE[dwDataSize + 5];
memset(pData, 0, dwDataSize + 5);
memcpy(pData, lpBuffer, dwDataSize);
switch(dwDataSize % 4096) {
case 4095:
++dwDataSize;
break;
case 4094:
bSendPendPacket = true;
break;
case 0:
default:
break;
}
crcValue = CRC_CCITT(pData, dwDataSize);
pData[dwDataSize] = (crcValue & 0xff00) >> 8;
pData[dwDataSize+1] = crcValue & 0x00ff;
dwDataSize += 2;
UINT nSendBytes = 0;
DWORD dwTotalSended = 0;
int iRet;
while(dwTotalSended < dwDataSize) {
nSendBytes = ( (dwDataSize - dwTotalSended) > 4096) ? 4096 : (dwDataSize - dwTotalSended);
iRet = libusb_control_transfer((libusb_device_handle *)m_pUsbHandle, 0x40, 0xC, 0, dwRequest, pData + dwTotalSended, nSendBytes, CMD_TIMEOUT);
if (iRet != (int)nSendBytes) {
if (m_log) {
m_log->Record("Error:RKU_DeviceRequest-->DeviceRequest vendor=0x%x failed, err=%d",dwRequest, iRet);
}
delete []pData;
return ERR_REQUEST_FAIL;
}
dwTotalSended += nSendBytes;
}
if(bSendPendPacket) {
BYTE ucFillByte = 0;
iRet = libusb_control_transfer((libusb_device_handle *)m_pUsbHandle, 0x40, 0xC, 0, dwRequest, &ucFillByte, 1, CMD_TIMEOUT);
if (iRet != 0) {
if (m_log) {
m_log->Record("Error:RKU_DeviceRequest-->DeviceRequest vendor=0x%x failed, err=%d", dwRequest, iRet);
}
delete []pData;
return ERR_REQUEST_FAIL;
}
}
delete []pData;
return ERR_SUCCESS;
}