-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrio.cpp
More file actions
1401 lines (1184 loc) · 34.3 KB
/
rio.cpp
File metadata and controls
1401 lines (1184 loc) · 34.3 KB
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///////////////////////////////////////////////////////////////////////////////
//
// rio.cpp
//
// v1.01 14/01/99 Initial launch.
//
// v1.02 18/01/99 Additional debug added for ioperm() call.
//
// v1.03 25/01/99 Added download support.
// Added delete support.
// Progress callback for upload/download file.
// Boland Turbo C++ v1.01 supported.
//
// v1.04 27/01/99 Win NT v4.0 supported, using RIOIO driver provided.
//
// v1.05 29/01/99 Added version field in dir header to be compatible
// with later Rio Manager v1.01 software.
// Added CheckPresent() member function.
// Support for Alpha platform.
//
// v1.06 11/03/99 Added support for external flash ram.
// Added initialization with bad block check.
// Added file re-ordering support.
//
///////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include <errno.h>
#include <sys/stat.h>
#include "std.h"
#include "binary.h"
#include "rio.h"
// platform dependencies
#if defined(_WINNT)
// MS VC++ v5.0 for WinNT v4
#include <windows.h>
#include <winioctl.h>
#include "rioioctl.h"
#define OUTPORT( p, v ) WinNTOutPort( p, v )
#define INPORT( p ) WinNTInPort( p )
#define CLOCK_SECOND 1000
#define DELETEARRAY delete[]
#define ID_DRIVER_VERSION 101
#elif defined(_WIN32)
// MS VC++ v5.0 for Win9x
#include <conio.h>
#define OUTPORT( p, v ) _outp( p, v )
#define INPORT( p ) _inp( p )
#define CLOCK_SECOND 1000
#define DELETEARRAY delete[]
#elif defined(__linux__)
// linux g++
#include <unistd.h>
#if defined(__alpha)
#include <sys/io.h>
#else
#include <sys/perm.h>
#endif
#include <asm/io.h>
#define OUTPORT(p,v) outb( v, p )
#define INPORT(p) inb( p )
#define CLOCK_SECOND 1000000
#define DELETEARRAY delete[]
#elif defined(__TURBOC__)
// turboc v1.01
#include <dos.h>
#define OUTPORT( p, v ) outp( p, v )
#define INPORT( p ) inp( p )
#define CLOCK_SECOND 18
#define DELETEARRAY delete
#else
// not supported
#error ! ! compiler/platform not supported ! !
#endif
// port offset constants
#define OFFSET_PORT_DATA 0
#define OFFSET_PORT_STATUS 1
#define OFFSET_PORT_CONTROL 2
// max tx/rx block retry
#define MAX_RETRY 3
// delay's
#define IODELAY(c) { for( int _iA=0; _iA<c; ++_iA ) INPORT(m_iPortStatus); }
#define DELAY(t) { long _lTime=clock()+t; while( _lTime > clock() ) ; }
// new, delete
#define NEWBLOCK( p ) { p = new UCHAR[ CRIO_SIZE_32KBLOCK ]; if ( !p ) { LogError( CRIO_ERROR_ALLOC, "new failed" ); return FALSE; } }
#define ZERONEWBLOCK( p ) { NEWBLOCK(p); memset(p, 0, CRIO_SIZE_32KBLOCK); }
#define DELETEBLOCK( p ) { if ( p ) { DELETEARRAY p; p = NULL; } }
// command out
#define COMMANDOUT(v1, v2, v3) { OUTPORT(m_iPortData, v1); OUTPORT(m_iPortControl, v2); OUTPORT(m_iPortControl, v3); }
// wait for reply
#define WAITNIBBLE( v1 ) { if (!WaitInput(v1)) return FALSE; }
#define WAITACK() { if (!WaitAck()) return FALSE; }
// block to use to check for presense
#define ID_32KBLOCK_CHECKPRESENT 0
// io delay constants
#if defined(_WINNT)
#define IODELAY_1 2000
#define IODELAY_2 2
#define IODELAY_3 10
#else
#define IODELAY_1 20000
#define IODELAY_2 2
#define IODELAY_3 100
#endif
///////////////////////////////////////////////////////////////////////////////
// if WinNT
#if defined(_WINNT)
// handle
static HANDLE m_hDriver;
// WinNT out port
void WinNTOutPort( int iPort, int iValue )
{
DWORD dwSizeReturn;
ULONG ulInput = ((ULONG)iPort-0x378) | ((ULONG)iValue << 16);
DeviceIoControl( m_hDriver, RIOIO_IOCTL_WRITE, &ulInput,
sizeof(long), NULL, 0, &dwSizeReturn, NULL );
}
// WinNT in port
int WinNTInPort( int iPort )
{
DWORD dwSizeReturn;
ULONG ulPort = iPort - 0x378;
ULONG ulData = 0;
DeviceIoControl( m_hDriver, RIOIO_IOCTL_READ, &ulPort, sizeof(ulPort),
&ulData, sizeof(char), &dwSizeReturn, NULL );
return (int)ulData;
}
#endif
///////////////////////////////////////////////////////////////////////////////
// if DOS
#if defined(__TURBOC__)
// get clock ticks
long clock( void )
{
return (long) (*(int far*)MK_FP( 0x40, 0x6c ));
}
#endif
///////////////////////////////////////////////////////////////////////////////
// return file only
static char* GetFile( char* pszPathFile )
{
int iLength = strlen( pszPathFile );
if ( !iLength )
return pszPathFile;
char* pc = pszPathFile + iLength - 1;
while( *pc != '\\' && *pc != '/' && *pc != ':' )
{
if ( pc == pszPathFile )
return pc;
--pc;
}
++pc;
return pc;
}
///////////////////////////////////////////////////////////////////////////////
// set/unset, constructors and destructors
void CRio::Unset( void )
{
// if WinNT
#if defined(_WINNT)
// close device file
if ( m_hDriver )
{
CloseHandle( m_hDriver );
m_hDriver = NULL;
}
#endif
}
BOOL CRio::Set( int iPortBase )
{
// cleanup any previous Set()
Unset();
// determine ports
m_iPortBase = iPortBase;
m_iPortData = m_iPortBase + OFFSET_PORT_DATA;
m_iPortStatus = m_iPortBase + OFFSET_PORT_STATUS;
m_iPortControl = m_iPortBase + OFFSET_PORT_CONTROL;
// if linux
#if defined(__linux__)
// request access to required ports
if ( ioperm(m_iPortBase, 3, 1) )
{
LogError( CRIO_ERROR_IOPRERM, "ioperm() failed, reason '%s'", SZERROR );
return FALSE;
}
#endif
// if WinNT
#if defined(_WINNT)
// open generic IO device
m_hDriver = CreateFile( "\\\\.\\RioDev", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );
if ( m_hDriver == INVALID_HANDLE_VALUE )
{
LogError( CRIO_ERROR_CREATEFILE, "CreateFile() failed, reason %ld\n", GetLastError() );
return FALSE;
}
// check version
DWORD dwSizeReturn;
ULONG ulVersion;
DeviceIoControl( m_hDriver, RIOIO_IOCTL_GETVERSION, NULL,
0, &ulVersion, sizeof(ulVersion), &dwSizeReturn, NULL );
if ( ulVersion != ID_DRIVER_VERSION )
{
LogError( CRIO_ERROR_DRIVERVERSION, "incorrect RioIO driver version, v%d.%d loaded, v%d.%d expected\n",
ulVersion/100, ulVersion%100, ID_DRIVER_VERSION/100, ID_DRIVER_VERSION%100 );
return FALSE;
}
#endif
return TRUE;
}
CRio::CRio()
{
// if WinNT
#if defined(_WINNT)
m_hDriver = NULL;
#endif
// default to use internal flash
m_bUseExternalFlash = FALSE;
}
CRio::~CRio()
{
Unset();
}
///////////////////////////////////////////////////////////////////////////////
// log error
void CRio::LogError( int iIDError, const char* pszFormat, ... )
{
va_list ArgPtr;
va_start( ArgPtr, pszFormat );
vsprintf( m_szError, pszFormat, ArgPtr );
va_end( ArgPtr );
m_iIDError = iIDError;
}
// find first free 32K block
UINT CRio::FindFirstFree32KBlock( void )
{
UINT uiA;
UCHAR* puc = m_cDirBlock.m_auc32KBlockUsed;
for( uiA=0; uiA<CRIO_MAX_32KBLOCK; ++uiA, ++puc )
{
if ( *puc == CRIO_ID_32KBLOCK_FREE )
return uiA;
}
return 0xffff;
}
// calculate checksum1 (checksum of directory header)
UINT CRio::CalculateChecksum1( void )
{
USHORT usChecksum = m_cDirBlock.m_cDirHeader.m_usChecksum1;
USHORT* paus = (USHORT*)&m_cDirBlock.m_cDirHeader;
for( int iA=0; iA<(sizeof(CDirHeader)/sizeof(short)); ++iA )
usChecksum -= *paus++;
return usChecksum;
}
// calculate checksum2 (checksum of directory entries, used flags and FAT)
UINT CRio::CalculateChecksum2( void )
{
USHORT usChecksum = 0;
USHORT* paus = (USHORT*)m_cDirBlock.m_acDirEntry;
int iSize = ( sizeof(CDirBlock) - sizeof(CDirHeader) ) / sizeof(short);
for( int iA=0; iA<iSize; ++iA )
usChecksum -= *paus++;
return usChecksum;
}
// wait for requested input from status port
BOOL CRio::WaitInput( int iValue )
{
long lTime = clock() + CLOCK_SECOND;
UCHAR ucRx;
while( lTime > clock() )
{
ucRx = INPORT( m_iPortStatus ) & 0xf8;
if ( ucRx == (int)iValue )
return TRUE;
}
return FALSE;
}
// wait for ack
BOOL CRio::WaitAck( void )
{
long lTime = clock() + CLOCK_SECOND;
while( lTime > clock() )
{
UCHAR ucRx = INPORT( m_iPortStatus );
if ( ucRx & 0x08 )
return TRUE;
}
return FALSE;
}
// get byte from status port
UINT CRio::GetDataByte( void )
{
// get hi nibble
OUTPORT( m_iPortControl, B_00000000 );
IODELAY( IODELAY_2 );
UCHAR ucRx = INPORT( m_iPortStatus );
UCHAR ucIn = ((ucRx & 0xf0) ^ 0x80) >> 4;
// get lo nibble and combine with previous nibble to make byte
OUTPORT( m_iPortControl, B_00000100 );
IODELAY( IODELAY_2 );
ucRx = INPORT( m_iPortStatus );
ucIn |= (ucRx & 0xf0) ^ 0x80;
// reverse bits in byte
UCHAR ucReversed = 0;
for( int iC=0; iC<8; ++iC )
{
ucReversed <<= 1;
ucReversed |= (ucIn & 1);
ucIn >>= 1;
}
return ucReversed;
}
// io intro
BOOL CRio::IOIntro( void )
{
OUTPORT( m_iPortControl, B_00000100 );
COMMANDOUT( B_10101000, B_00001100, B_00000100 );
OUTPORT( m_iPortControl, B_00000000 );
IODELAY( IODELAY_1 );
OUTPORT( m_iPortControl, B_00000100 );
IODELAY( IODELAY_1 );
COMMANDOUT( B_10101101, B_00001100, B_00000100 );
COMMANDOUT( B_01010101, B_00000000, B_00000100 );
COMMANDOUT( B_10101110, B_00001100, B_00000100 );
COMMANDOUT( B_10101010, B_00000000, B_00000100 );
COMMANDOUT( B_10101000, B_00001100, B_00000100 );
OUTPORT( m_iPortControl, B_00000000 );
IODELAY( IODELAY_1 );
OUTPORT( m_iPortControl, B_00000100 );
IODELAY( IODELAY_1 );
return TRUE;
}
// io outro
BOOL CRio::IOOutro( void )
{
COMMANDOUT( B_10101101, B_00001100, B_00000100 );
COMMANDOUT( B_11111111, B_00000000, B_00000100 );
OUTPORT( m_iPortData, B_00000000 );
return TRUE;
}
// tx 32K block retry
BOOL CRio::Tx32KBlockRetry( void* pv, UINT uiPos32KBlock, UINT uiPos32KBlockPrev,
UINT uiPos32KBlockNext )
{
ULONG ulPos512ByteBlock;
ULONG ulPosHi;
ULONG ulPosMid;
ULONG ulPosLo;
int iA, iB;
// io intro
if ( !IOIntro() )
return FALSE;
// prepare 32K block
int iC = 1;
if ( m_bUseExternalFlash )
iC = 2;
for( iA=0; iA<4; iA+=iC )
{
ulPos512ByteBlock = ((ULONG)uiPos32KBlock * 32768 + (ULONG)iA * 8192) / 512;
ulPosLo = ulPos512ByteBlock & 0xff;
if ( m_bUseExternalFlash )
{
ulPosHi = 0x04;
ulPosMid = (ulPos512ByteBlock & 0xff00) >> 8;
}
else
{
ulPosHi = ulPos512ByteBlock / 16384;
ulPosMid = (ulPos512ByteBlock & 0x3f00) >> 8;
}
COMMANDOUT( B_10101011, B_00001100, B_00000100 );
COMMANDOUT( ulPosHi, B_00000000, B_00000100 );
COMMANDOUT( B_10100001, B_00001100, B_00000100 );
COMMANDOUT( B_01100000, B_00000000, B_00000100 );
COMMANDOUT( B_10100010, B_00001100, B_00000100 );
COMMANDOUT( ulPosLo, B_00000000, B_00000100 );
COMMANDOUT( ulPosMid, B_00000000, B_00000100 );
COMMANDOUT( B_10100001, B_00001100, B_00000100 );
COMMANDOUT( B_11010000, B_00000000, B_00000100 );
WAITACK();
COMMANDOUT( B_10100001, B_00001100, B_00000100 );
COMMANDOUT( B_01110000, B_00000000, B_00000100 );
COMMANDOUT( B_10100000, B_00001100, B_00000100 );
OUTPORT( m_iPortControl, B_00000000 );
IODELAY( IODELAY_3 );
OUTPORT( m_iPortControl, B_00000100 );
IODELAY( IODELAY_3 );
}
// send 32K in 512 byte chunks
UCHAR* pauc = (UCHAR*)pv;
for( iA=0; iA<(32768/512); ++iA, pauc+=512 )
{
ulPos512ByteBlock = ((ULONG)uiPos32KBlock * 32768 + (ULONG)iA * 512) / 512;
ulPosLo = ulPos512ByteBlock & 0xff;
if ( m_bUseExternalFlash )
{
ulPosHi = 0x04;
ulPosMid = (ulPos512ByteBlock & 0xff00) >> 8;
}
else
{
ulPosHi = ulPos512ByteBlock / 16384;
ulPosMid = (ulPos512ByteBlock & 0x3f00) >> 8;
}
// issue upload 512 byte block command
COMMANDOUT( B_10101011, B_00001100, B_00000100 );
COMMANDOUT( ulPosHi, B_00000000, B_00000100 );
COMMANDOUT( B_10100001, B_00001100, B_00000100 );
COMMANDOUT( B_10000000, B_00000000, B_00000100 );
COMMANDOUT( B_10100010, B_00001100, B_00000100 );
COMMANDOUT( B_00000000, B_00000000, B_00000100 );
COMMANDOUT( ulPosLo, B_00000000, B_00000100 );
COMMANDOUT( ulPosMid, B_00000000, B_00000100 );
COMMANDOUT( B_10100011, B_00001100, B_00000100 );
// create checksum of 512 byte block
USHORT usChecksum = 0;
USHORT* paus = (USHORT*)pauc;
for( iB=0; iB<(512/sizeof(short)); ++iB, ++paus )
usChecksum -= *paus;
// clock out data
#if defined(_WINNT)
{
DWORD dwSizeReturn;
DeviceIoControl( m_hDriver, RIOIO_IOCTL_WRITEBLOCK, pauc, 512,
NULL, 0, &dwSizeReturn, NULL );
}
#else
UCHAR* pauc2 = pauc;
for( iB=0; iB<512; ++iB, ++pauc2 )
{
OUTPORT( m_iPortData, (*pauc2) );
if ( !(iB & 1) )
OUTPORT( m_iPortControl, B_00000000 );
else
OUTPORT( m_iPortControl, B_00000100 );
IODELAY( 1 );
}
#endif
// prepare end of block
CEnd512ByteBlock cEnd512ByteBlock;
memset( &cEnd512ByteBlock, 0, sizeof(CEnd512ByteBlock) );
cEnd512ByteBlock.m_usChecksum = usChecksum;
if ( uiPos32KBlockNext == 0xffff )
{
cEnd512ByteBlock.m_ulPos512ByteBlockNextMult256 = 0xffffffff;
cEnd512ByteBlock.m_ucPos8192KBlockNext1 = 0xff;
cEnd512ByteBlock.m_ucPos8192KBlockNext2 = 0xff;
}
else
{
cEnd512ByteBlock.m_ulPos512ByteBlockNextMult256 = ((ULONG)uiPos32KBlockNext * 64) * 256;
cEnd512ByteBlock.m_ucPos8192KBlockNext1 = uiPos32KBlockNext / 256;
cEnd512ByteBlock.m_ucPos8192KBlockNext2 = uiPos32KBlockNext / 256;
if ( m_bUseExternalFlash )
{
cEnd512ByteBlock.m_ulPos512ByteBlockNextMult256 += 0x01000000;
cEnd512ByteBlock.m_ucPos8192KBlockNext1 = 0;
cEnd512ByteBlock.m_ucPos8192KBlockNext2 = 0;
}
}
if ( uiPos32KBlockPrev == 0xffff )
{
cEnd512ByteBlock.m_ulPos512ByteBlockPrevMult256 = 0xffffffff;
cEnd512ByteBlock.m_ucPos8192KBlockPrev1 = 0xff;
cEnd512ByteBlock.m_ucPos8192KBlockPrev2 = 0xff;
cEnd512ByteBlock.m_usPos32KBlockPrevMult256 = 0xffff;
}
else
{
cEnd512ByteBlock.m_ulPos512ByteBlockPrevMult256 = ((ULONG)uiPos32KBlockPrev * 64) * 256;
cEnd512ByteBlock.m_ucPos8192KBlockPrev1 = uiPos32KBlockPrev / 256;
cEnd512ByteBlock.m_ucPos8192KBlockPrev2 = uiPos32KBlockPrev / 256;
if ( m_bUseExternalFlash )
{
cEnd512ByteBlock.m_ulPos512ByteBlockPrevMult256 += 0x01000000;
cEnd512ByteBlock.m_ucPos8192KBlockPrev1 = 0;
cEnd512ByteBlock.m_ucPos8192KBlockPrev2 = 0;
}
cEnd512ByteBlock.m_usPos32KBlockPrevMult256 = uiPos32KBlockPrev * 256;
}
// output end of block
#if defined(_WINNT)
{
DWORD dwSizeReturn;
DeviceIoControl( m_hDriver, RIOIO_IOCTL_WRITEBLOCK, &cEnd512ByteBlock,
sizeof(CEnd512ByteBlock), NULL, 0, &dwSizeReturn, NULL );
}
#else
pauc2 = (UCHAR*)&cEnd512ByteBlock;
for( iB=0; iB<sizeof(CEnd512ByteBlock); ++iB, ++pauc2 )
{
OUTPORT( m_iPortData, (*pauc2) );
if ( !(iB & 1) )
OUTPORT( m_iPortControl, B_00000000 );
else
OUTPORT( m_iPortControl, B_00000100 );
IODELAY( 1 );
}
#endif
// end of tx
COMMANDOUT( B_10100001, B_00001100, B_00000100 );
COMMANDOUT( B_00010000, B_00000000, B_00000100 );
WAITACK();
COMMANDOUT( B_10100001, B_00001100, B_00000100 );
COMMANDOUT( B_01110000, B_00000000, B_00000100 );
COMMANDOUT( B_10100000, B_00001100, B_00000100 );
OUTPORT( m_iPortControl, B_00000000 );
IODELAY( IODELAY_3 );
OUTPORT( m_iPortControl, B_00000100 );
IODELAY( IODELAY_3 );
}
return TRUE;
}
// tx 32K block
BOOL CRio::Tx32KBlock( void* pv, UINT uiPos32KBlock, UINT uiPos32KBlockPrev,
UINT uiPos32KBlockNext )
{
int iRetry = 0;
while( iRetry < MAX_RETRY )
{
if ( Tx32KBlockRetry(pv, uiPos32KBlock, uiPos32KBlockPrev, uiPos32KBlockNext) )
return TRUE;
DELAY( CLOCK_SECOND );
++iRetry;
}
LogError( CRIO_ERROR_TXBLOCKRETRY, "too many retries for tx block" );
return FALSE;
}
// rx 32K block retry
BOOL CRio::Rx32KBlockRetry( void* pv, UINT uiPos32KBlock )
{
ULONG ulPos512ByteBlock;
ULONG ulPosHi;
ULONG ulPosMid;
ULONG ulPosLo;
int iA, iB;
// io intro
if ( !IOIntro() )
return FALSE;
// get 32K in 512 byte chunks
UCHAR* pauc = (UCHAR*)pv;
for( iA=0; iA<(32768/512); ++iA, pauc+=512 )
{
ulPos512ByteBlock = ((ULONG)uiPos32KBlock * 32768 + (ULONG)iA * 512) / 512;
ulPosLo = ulPos512ByteBlock & 0xff;
if ( m_bUseExternalFlash )
{
ulPosHi = 0x04;
ulPosMid = (ulPos512ByteBlock & 0xff00) >> 8;
}
else
{
ulPosHi = ulPos512ByteBlock / 16384;
ulPosMid = (ulPos512ByteBlock & 0x3f00) >> 8;
}
// issue download 512 byte block command
COMMANDOUT( B_10101011, B_00001100, B_00000100 );
COMMANDOUT( ulPosHi, B_00000000, B_00000100 );
COMMANDOUT( B_10100001, B_00001100, B_00000100 );
COMMANDOUT( B_00000000, B_00000000, B_00000100 );
COMMANDOUT( B_10100010, B_00001100, B_00000100 );
COMMANDOUT( B_00000000, B_00000000, B_00000100 );
COMMANDOUT( ulPosLo, B_00000000, B_00000100 );
COMMANDOUT( ulPosMid, B_00000000, B_00000100 );
WAITACK();
COMMANDOUT( B_10100000, B_00001100, B_00000100 );
// clock in data
#if defined(_WINNT)
{
DWORD dwSizeReturn;
DeviceIoControl( m_hDriver, RIOIO_IOCTL_READBLOCK, pauc, 512,
pauc, 512, &dwSizeReturn, NULL );
}
#else
for( iB=0; iB<512; ++iB )
*(pauc+iB) = GetDataByte();
#endif
// clock in 16 bytes which are ignored
for( iB=0; iB<16; ++iB )
GetDataByte();
// delay
WAITACK();
}
return TRUE;
}
// rx 32K block
BOOL CRio::Rx32KBlock( void* pv, UINT uiPos32KBlock )
{
int iRetry = 0;
while( iRetry < MAX_RETRY )
{
if ( Rx32KBlockRetry(pv, uiPos32KBlock) )
return TRUE;
DELAY( CLOCK_SECOND );
++iRetry;
}
LogError( CRIO_ERROR_RXBLOCKRETRY, "too many retries for rx block" );
return FALSE;
}
// mark bad blocks
BOOL CRio::MarkBadBlocks( BOOL (*pfProgress)(int iPos, int iCount) )
{
// create temp block
UCHAR* paucBlock;
NEWBLOCK( paucBlock );
// block count
USHORT usPos32KBlockEnd;
if ( m_bUseExternalFlash )
usPos32KBlockEnd = m_uiCount32KBlockAvailableExternal;
else
usPos32KBlockEnd = CRIO_COUNT_32KBLOCKIN32M;
// assume directory block is ok
m_cDirBlock.m_auc32KBlockUsed[ 0 ] = CRIO_ID_32KBLOCK_FREE;
// process all blocks (except directory block)
int iCount32KBlockBad = 0;
USHORT usPos32KBlock;
for( usPos32KBlock=1; usPos32KBlock<usPos32KBlockEnd; ++usPos32KBlock )
{
UINT uiB;
// progress callback
if ( pfProgress )
{
if ( !pfProgress(usPos32KBlock, usPos32KBlockEnd) )
{
LogError( CRIO_ERROR_INTERRUPTED, "operation interrupted" );
break;
}
}
// default to bad block
UCHAR ucState = CRIO_ID_32KBLOCK_BAD;
// create and send test block 1
memset( paucBlock, B_10101010, CRIO_SIZE_32KBLOCK );
if ( !Tx32KBlock(paucBlock, usPos32KBlock, 0, 0) )
break;
// get test block 1
if ( !Rx32KBlock(paucBlock, usPos32KBlock) )
break;
// check test block 1
for( uiB=0; uiB<CRIO_SIZE_32KBLOCK; ++uiB )
{
if ( *(paucBlock+uiB) != B_10101010 )
break;
}
// if ok
if ( uiB == CRIO_SIZE_32KBLOCK )
{
// create and send test block 2
memset( paucBlock, B_01010101, CRIO_SIZE_32KBLOCK);
if ( !Tx32KBlock(paucBlock, usPos32KBlock, 0, 0) )
break;
// get test block 2
if ( !Rx32KBlock(paucBlock, usPos32KBlock) )
break;
// check test block 2
for( uiB=0; uiB<CRIO_SIZE_32KBLOCK; ++uiB )
{
if ( *(paucBlock+uiB) != B_01010101 )
break;
}
// if ok, block ok
if ( uiB == CRIO_SIZE_32KBLOCK )
ucState = CRIO_ID_32KBLOCK_FREE;
}
// store block state
m_cDirBlock.m_auc32KBlockUsed[ usPos32KBlock ] = ucState;
// adjust bad block count
if ( ucState == CRIO_ID_32KBLOCK_BAD )
++iCount32KBlockBad;
}
// if tx,rx block error or interrupted
if ( usPos32KBlock < usPos32KBlockEnd )
return FALSE;
// store blocks used and bad block count
m_cDirBlock.m_cDirHeader.m_usCount32KBlockUsed = iCount32KBlockBad;
m_cDirBlock.m_cDirHeader.m_usCount32KBlockBad = iCount32KBlockBad;
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////
// operations
void CRio::UseExternalFlash( BOOL bUseExternalFlash )
{
m_bUseExternalFlash = bUseExternalFlash;
}
BOOL CRio::CheckPresent( void )
{
long lA;
// create temp block
UCHAR* paucBlock;
NEWBLOCK( paucBlock );
// get a block
BOOL bResult = Rx32KBlock( paucBlock, ID_32KBLOCK_CHECKPRESENT );
if ( bResult )
{
// make copy of block
UCHAR* paucBlock2;
NEWBLOCK( paucBlock2 );
memcpy( paucBlock2, paucBlock, CRIO_SIZE_32KBLOCK );
// amend block
for( lA=0; lA<CRIO_SIZE_32KBLOCK; ++lA )
paucBlock2[ lA ] = (UCHAR)lA;
// save block
bResult = Tx32KBlock( paucBlock2, ID_32KBLOCK_CHECKPRESENT, 0, 0 );
if ( bResult )
{
// get block again
memset( paucBlock2, 0, CRIO_SIZE_32KBLOCK );
bResult = Rx32KBlock( paucBlock2, ID_32KBLOCK_CHECKPRESENT );
if ( bResult )
{
// compare
for( lA=0; lA<CRIO_SIZE_32KBLOCK; ++lA )
{
if ( paucBlock2[lA] != (UCHAR)lA )
break;
}
// if block different
if ( lA < CRIO_SIZE_32KBLOCK )
bResult = FALSE;
// else block ok
else
{
//restore original block
bResult = Tx32KBlock( paucBlock, ID_32KBLOCK_CHECKPRESENT, 0, 0 );
}
}
}
DELETEBLOCK( paucBlock2 );
}
DELETEBLOCK( paucBlock );
// if error
if ( !bResult )
{
LogError( CRIO_ERROR_DEVICENOTFOUND, "device not found" );
return FALSE;
}
// io intro
if ( !IOIntro() )
return FALSE;
// issue read id command
COMMANDOUT( 0xAB, B_00001100, B_00000100 );
COMMANDOUT( 0x04, B_00000000, B_00000100 );
COMMANDOUT( 0xA1, B_00001100, B_00000100 );
COMMANDOUT( 0x90, B_00000000, B_00000100 );
COMMANDOUT( 0xA2, B_00001100, B_00000100 );
COMMANDOUT( 0x00, B_00000000, B_00000100 );
COMMANDOUT( 0xA0, B_00001100, B_00000100 );
// determine mem size in no of 512 byte blocks
GetDataByte();
UCHAR ucDevice = GetDataByte();
ULONG ulCount512ByteBlock;
switch( ucDevice )
{
case 0xEA:
case 0x64: ulCount512ByteBlock = 4096; break;
case 0xE3:
case 0xE5: ulCount512ByteBlock = 8192; break;
case 0xE6: ulCount512ByteBlock = 16384; break;
case 0x73: ulCount512ByteBlock = 32768; break;
case 0x75: ulCount512ByteBlock = 65536; break;
default: ulCount512ByteBlock = 0; break;
};
// convert page count to 32K block count
m_uiCount32KBlockAvailableExternal = (ulCount512ByteBlock * 512) / CRIO_SIZE_32KBLOCK;
return TRUE;
}
CDirEntry* CRio::FindFile( char* pszFile )
{
// search directory entries for matching filename
int iCountEntry = m_cDirBlock.m_cDirHeader.m_usCountEntry;
CDirEntry* pDirEntry = m_cDirBlock.m_acDirEntry;
for( int iA=0; iA<iCountEntry; ++iA, ++pDirEntry )
{
if ( !strcmp(pszFile, pDirEntry->m_szName) )
return pDirEntry;
}
return NULL;
}
BOOL CRio::Initialize( BOOL bMarkBadBlock, BOOL (*pfProgress)(int iPos, int iCount) )
{
CDirHeader& cDirHeader = m_cDirBlock.m_cDirHeader;
// init directory header
memset( &m_cDirBlock.m_cDirHeader, 0, sizeof(m_cDirBlock.m_cDirHeader) );
// set version (so compatible with Rio Manager v1.01)
cDirHeader.m_usVersion = 0x0100;
// init directory entries
memset( &m_cDirBlock.m_acDirEntry, 0, sizeof(m_cDirBlock.m_acDirEntry) );
// init block used flags
memset( &m_cDirBlock.m_auc32KBlockUsed, CRIO_ID_32KBLOCK_FREE, sizeof(m_cDirBlock.m_auc32KBlockUsed) );
// init FAT
memset( &m_cDirBlock.m_ausFAT, 0, sizeof(m_cDirBlock.m_ausFAT) );
// if mark bad block request
if ( bMarkBadBlock )
{
if ( !MarkBadBlocks(pfProgress) )
return FALSE;
}
// available blocks
if ( m_bUseExternalFlash )
cDirHeader.m_usCount32KBlockAvailable = m_uiCount32KBlockAvailableExternal;
else
cDirHeader.m_usCount32KBlockAvailable = CRIO_COUNT_32KBLOCKIN32M;
// blocks remaining (taking into account bad blocks)
cDirHeader.m_usCount32KBlockRemaining =
cDirHeader.m_usCount32KBlockAvailable -
cDirHeader.m_usCount32KBlockBad;
return TRUE;
}
BOOL CRio::RemoveFile( char* pszFile )
{
// get directory entry for file
CDirEntry* pDirEntry = FindFile( pszFile );
if ( !pDirEntry )
{
LogError( CRIO_ERROR_FILENOTFOUND, "file '%s' not present on device", pszFile );
return FALSE;
}
// free FAT and blocks used
USHORT usPos32KBlock = pDirEntry->m_usPos32KBlock;
while( usPos32KBlock )
{
m_cDirBlock.m_auc32KBlockUsed[ usPos32KBlock ] = CRIO_ID_32KBLOCK_FREE;
USHORT usTemp = m_cDirBlock.m_ausFAT[ usPos32KBlock ];
m_cDirBlock.m_ausFAT[ usPos32KBlock ] = 0;
usPos32KBlock = usTemp;
}
// adjust directory header
CDirHeader& cDirHeader = m_cDirBlock.m_cDirHeader;
--cDirHeader.m_usCountEntry;
cDirHeader.m_usCount32KBlockUsed -= pDirEntry->m_usCount32KBlock;
cDirHeader.m_usCount32KBlockRemaining += pDirEntry->m_usCount32KBlock;
// clear directory entry
memset( pDirEntry, 0, sizeof(CDirEntry) );
// shuffle directory entries
int iPosEntry = pDirEntry - m_cDirBlock.m_acDirEntry;
int iCount = (int)cDirHeader.m_usCountEntry - iPosEntry;
for( int iA=0; iA<iCount; ++iA )
{
memcpy(
&m_cDirBlock.m_acDirEntry[ iPosEntry+iA ],
&m_cDirBlock.m_acDirEntry[ iPosEntry+iA+1 ],
sizeof(CDirEntry)
);
}
return TRUE;
}
BOOL CRio::RemoveAllFiles( void )
{