-
Notifications
You must be signed in to change notification settings - Fork 1
/
rigolif.c
674 lines (621 loc) · 16.1 KB
/
rigolif.c
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
#define _CRT_SECURE_NO_WARNINGS
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#pragma comment(lib,"Ws2_32.lib")
typedef enum{
Work_view,
Work_read,
Work_call,
Work_load,
Work_write
}WorkMode;
static WorkMode Wm;
static BOOL SilentFlg = FALSE;
static unsigned long Address = 1;
static unsigned long Length = 0;
static unsigned long Value = 0;
static unsigned long Param1 = 0;
static unsigned long Param2 = 0;
static char *Filename = NULL;
static FILE *fIO = NULL;
static const int timeoutIO = 200;
static const int timeoutRun = 2000;
#define READ_MAX_QUANT 50
#pragma pack(push,1)
// 12 bytes
typedef struct{
/*
? - ask id (for compatible rigol)
C - call function (*Address)(Param1, Param2)
R - Read Memory at Address (max 3 dword, size in Param1)
W - Write Memory at Address (source in Param1)
Warning! DWORD align !!!
*/
char Cmd;
char Terminator;
unsigned short Param2;
unsigned long Address;
unsigned long Param1;
}RigolReqBlock;
#pragma pack(pop)
//#if sizeof(RigolReqBlock) != 0xC
//#error Bad packet Size
//#endif
#define NUM_WAIT_REP (3)
static int pascal NetSelAndRecv(
SOCKET s,
char *buf, int len,
unsigned int Timeout)
{
fd_set readfds;
struct timeval timeout;
int RetVal;
int WaitRepeat = 0;
readfds.fd_array[0] = s;
readfds.fd_count = 1;
timeout.tv_sec = Timeout / 1000;
timeout.tv_usec = 1000 * (Timeout % 1000);
if(1 != (RetVal = select(0, &readfds, NULL, NULL, &timeout))){
return(0);
}
return(recv(s, buf, len, 0));
}
static int pascal NetSelAndRecvfrom(
SOCKET s,
char *buf, int len,
unsigned int Timeout,
struct sockaddr *from,
int *fromlen)
{
fd_set readfds;
struct timeval timeout;
int RetVal;
readfds.fd_array[0] = s;
readfds.fd_count = 1;
timeout.tv_sec = Timeout / 1000;
timeout.tv_usec = 1000 * (Timeout % 1000);
if(1 != (RetVal = select(0, &readfds, NULL, NULL, &timeout))) return(RetVal);
return(recvfrom(s, buf, len, 0, from, fromlen));
}
// Ïîëó÷èòü ñïèñîê âñåõ
static int FindRigol(SOCKADDR *RigolAddr)
{
SOCKET SCK;
DWORD optval;
SOCKADDR SA;
int Len;
unsigned char Answ[1024];
int FromLen;
int NoAnswer;
static const char AskCmd[]="?";
SCK = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if(INVALID_SOCKET == SCK){
return(-1);
}
optval = 1;
setsockopt(SCK, SOL_SOCKET, SO_BROADCAST, (void *)&optval, sizeof(optval));
// Ïîñûëêà çàïðîñà íà îòêëèê
memset(&SA,0,sizeof(SA));
// Øèðèêîâåùàòåëüíî - 255.255.255.255
*(DWORD *)&SA.sa_data[2] = -1;
// Öåëåâîé ïîðò
*(WORD *)&SA.sa_data[0] = htons(6000u);
SA.sa_family = AF_INET;
// ïîøëåì ?
Len = sendto(SCK, AskCmd, sizeof(AskCmd),0, &SA, sizeof(SA));
if(Len != sizeof(AskCmd)){
closesocket(SCK);
return(-1);
}
NoAnswer = 5;
do{
// Ïðèíèìàåì îòâåòû îòêëèêíóâøèõñÿ ïëàò
FromLen = sizeof(SA);
Len = NetSelAndRecvfrom(SCK, Answ, sizeof(Answ), 100u, RigolAddr, &FromLen);
if(Len > 0){
Answ[Len]=0;
if(!SilentFlg) printf("Autodetect answer: [%s] from %u.%u.%u.%u\n",
Answ,
(unsigned char)RigolAddr->sa_data[2],
(unsigned char)RigolAddr->sa_data[3],
(unsigned char)RigolAddr->sa_data[4],
(unsigned char)RigolAddr->sa_data[5]
);
closesocket(SCK);
return(0);
}
NoAnswer--;
}while(NoAnswer > 0);
closesocket(SCK);
if(!SilentFlg) printf("No Rigol detected\n");
return(-2);
}
// <=0 - error
int RigolExchange(SOCKET Sock, RigolReqBlock *SrcBuf, void *Dst, size_t MaxLen, int Timeout)
{
int RealSize;
// Çàøëåì óïðàâëÿþùèé áëîê
RealSize = send(Sock, (void *) SrcBuf, sizeof(RigolReqBlock), 0);
if( RealSize != sizeof(RigolReqBlock) ){
if(!SilentFlg) printf("TX Error (%d/%d), code =%d\n", RealSize, sizeof(RigolReqBlock), WSAGetLastError());
return(-1);
}
// Ïðîâåðèì îòâåò ñ òàéìàóòîì
return(NetSelAndRecv(Sock, Dst, MaxLen, Timeout));
}
int RigolGetIdent(SOCKET Sock, char *TextBuffer, int MaxLen)
{
int Len;
RigolReqBlock Req;
Req.Cmd = '?';
Req.Terminator = 0;
Len = RigolExchange(Sock, &Req, TextBuffer, MaxLen - 1, timeoutIO);
if(Len <=0){
if(!SilentFlg) printf("Get Ident Io Error, %d\n", Len);
return(-1);
}
if(Len > MaxLen - 1)Len = MaxLen - 1;
TextBuffer[Len]=0;
return(Len);
}
int RigolWriteDword(SOCKET Sock, unsigned long Address, unsigned long Value)
{
int Len;
RigolReqBlock Req;
Req.Cmd = 'W';
Req.Terminator = 0;
Req.Address = Address;
Req.Param1 = Value;
Len = RigolExchange(Sock, &Req, &Req, sizeof(Req), timeoutIO);
if(Len != sizeof(Req)){
if(!SilentFlg) printf("Error on write %08.8X to %08.8X, %d\n", Value, Address, Len);
return(-1);
}
if((Req.Address != Address) ||
((Req.Param1 ^ ~Value) !=0) ||
(Req.Cmd != 'W')){
if(!SilentFlg) printf("Bad answer packet on write %08.8X to %08.8X\n", Value, Address);
return(-2);
}
return(1);
}
static unsigned long RepAddress = 0;
static int RepPktCntr = 0;
// ×èòàòü áëîê ïàìÿòè
int RigolReadDword(SOCKET Sock, unsigned long Address, unsigned int Num, void *AnswerBuffer)
{
int Len;
int RepeatCounter = 0;
RigolReqBlock Req;
unsigned long AnswerBuf[READ_MAX_QUANT + 2];
while(Num > 0){
int CurLen;
CurLen = Num;
if(CurLen > READ_MAX_QUANT) CurLen = READ_MAX_QUANT;
for(;;){
Req.Cmd = 'R';
Req.Terminator = 0;
Req.Address = Address;
Req.Param2 = CurLen - 1;
Len = RigolExchange(Sock, &Req, AnswerBuf, sizeof(AnswerBuf), timeoutIO);
Repp:
if(Len == (CurLen + 2) * 4) break;
if(RepeatCounter ++ > 5){
if(!SilentFlg) printf("Fatal error on read %ld dwords from %08.8X, %d\n",
Req.Param2 + 1 , Address, Len);
return(-1);
}
RepPktCntr++;
RepAddress = Address;
}
// Check For Duplicate
if((RepPktCntr > 0) && (RepAddress != Address)){
if( (AnswerBuf[0] == RepAddress) ||
(AnswerBuf[1] == CurLen - 1)){
// Ñòàðûé ïîâòîðíûé ïàêåò - âûêèíåì è çàïóñòèì íîâîå ÷òåíèå
RepPktCntr --;
Len = NetSelAndRecv(Sock, (void*)AnswerBuf, sizeof(AnswerBuf), 100);
goto Repp;
}
}
if( (AnswerBuf[0] != Address) ||
(AnswerBuf[1] != CurLen - 1)){
if(RepeatCounter ++ > 5){
if(!SilentFlg) printf("Fatal, bad answer packet on read %d dwords from %08.8X (%d@%X)\n",
CurLen, Address,
AnswerBuf[1] + 1,
AnswerBuf[0]
);
return(-1);
}
// repeat request
continue;
}
// Óøëè çà ïîâòîðíûé ïàêåò - ñáðîñèì ñ÷åò÷èê
if(Address > RepAddress) RepPktCntr = 0;
Num -= CurLen;
Address += CurLen * 4;
memcpy(AnswerBuffer, AnswerBuf + 2, CurLen * 4);
AnswerBuffer = ((unsigned char *)AnswerBuffer) + (CurLen * 4);
RepeatCounter = 0;
}
return(0);
}
int RigolCallFunc(SOCKET Sock, unsigned long Address, unsigned long Param1, unsigned short Param2, unsigned long *Result)
{
int Len;
RigolReqBlock Req;
Req.Cmd = 'C';
Req.Terminator = 0;
Req.Address = Address;
Req.Param1 = Param1;
Req.Param2 = Param2;
Len = RigolExchange(Sock, &Req, &Req, sizeof(Req), timeoutRun);
if(Len != sizeof(Req)){
if(!SilentFlg) printf("Error on call %08.8X(%08.8X,%08.8X) = %d\n", Address, Param1, Param2, Len);
return(-1);
}
if((Req.Address != Address) ||
(Req.Cmd != 'C')){
if(!SilentFlg) printf("Bad answer packet on call %08.8X\n", Address);
return(-2);
}
if(NULL != Result) *Result = Req.Param1;
return(1);
}
static void HelpOut(void)
{
printf(
"Usage: rigolif mode [keys]\n"
"mode: \n"
" v[iew] - show scope ID\n"
" r[read] - read memory from scope\n"
" w[rite] - write data to scope memory\n"
" c[all] - call scope function and dump result\n"
" l[oad] - load plugin and run in scope\n"
"keys:\n"
" -i<Address> - set scope IP address\n"
" -s - silent mode\n"
" -a<Address> - setup target address\n"
" -v<Value> - setup value to write\n"
" -p1<Param1> - first function parameter\n"
" -p2<Param2> - second function parameter\n"
" -f<File> - File for read/write/run\n"
" -l<Len> - Length in dwords\n"
);
}
static unsigned long GetValue(char *Text)
{
char *EndTxt=NULL;
unsigned long Val;
Val = strtoul(Text, &EndTxt, 0);
if((EndTxt == NULL) || (EndTxt[0] !=0)){
if(!SilentFlg) printf("Bad value: [%s]\n", Text);
HelpOut();
exit(23);
}
return(Val);
}
static int LoadFile2Scope(SOCKET Sock, FILE *Src, unsigned long Address, unsigned long Length)
{
while(Length > 0){
int Len;
unsigned long Val = 0;
fread(&Val, 1, 4, Src);
if(0 >= (Len = RigolWriteDword(Sock, Address, Val))){
if(!SilentFlg) printf("Write error at address %08.8lX\n", Address);
return(-1);
}
Address +=4;
Length -=4;
}
return(1);
}
static unsigned long GetFSize(FILE *f)
{
long CurPos;
long TailPos;
CurPos = ftell(f);
fseek(f, 0, SEEK_END);
TailPos = ftell(f);
fseek(f, CurPos, SEEK_SET);
if(TailPos < 0) return(0);
return(TailPos);
}
int main(char argc, char **argv)
{
int RetCode = 0;
SOCKET Sock;
struct sockaddr_in SAddr;
struct hostent * he = NULL;
unsigned char Answer[READ_MAX_QUANT * 4];
int Len;
BOOL LongOperation = FALSE;
signed long ItemsCounter;
if(argc < 2){
HelpOut();
return(1);
}
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
if(!SilentFlg) printf("Winsock init err %d\n", err);
return 1;
}
}
memset(&SAddr, 0, sizeof(SAddr));
// parse keys
{
int CurKey;
switch(argv[1][0]){
case 'v':
case 'V': Wm = Work_view;
break;
case 'r':
case 'R': Wm = Work_read;
break;
case 'w':
case 'W': Wm = Work_write;
break;
case 'c':
case 'C': Wm = Work_call;
break;
case 'l':
case 'L': Wm = Work_load;
break;
default:
if(!SilentFlg) printf("Unknown work mode [%s]\n", argv[1]);
HelpOut();
return(1);
}
for(CurKey=2;CurKey < argc;CurKey++){
char *Key;
Key = argv[CurKey];
switch(Key[0]){
case '-':
case '/': break;
default:
if(!SilentFlg) printf("Bad key [%s]\n", Key);
HelpOut();
return(1);
}
switch(Key[1]){
// " -i<Address> - set scope IP address\n"
case 'i':
case 'I': he = gethostbyname(Key + 2);
if(NULL == he){
printf("Unable to get IP address for [%s]\n", Key + 2);
HelpOut();
return(1);
}
break;
// " -s - silent mode\n"
case 's':
case 'S': SilentFlg = TRUE;
break;
//" -a<Address> - setup target address\n"
case 'a':
case 'A': Address = GetValue(Key + 2);
break;
// " -v<Value> - setup value to write\n"
case 'v':
case 'V': if(NULL != Filename){
printf("File-mode already selected\n");
HelpOut();
return(1);
}
Value = GetValue(Key + 2);
break;
// " -p1<Param1> - first function parameter\n"
// " -p2<Param2> - second function parameter\n"
case 'p':
case 'P':
switch(Key[2]){
case '1': Param1 = GetValue(Key + 3); break;
case '2': Param2 = GetValue(Key + 3); break;
default:
printf("Bad parameter key [%s]\n", Key);
HelpOut();
return(1);
}
break;
// " -f<File> - File for read/write/run\n"
case 'F':
case 'f': Filename = Key + 2;
break;
// " -l<Len> - Length in dwords\n"
case 'L':
case 'l': Length = GetValue(Key + 2);
break;
default:
printf("Unknown key [%s]\n", Key);
HelpOut();
return(1);
}
}
}
// check params
switch(Wm){
case Work_view: break;
case Work_read: if((Address & 3) || (Length < 1)){
printf("Bad read parametrs\n");
HelpOut();
return(1);
}
if(NULL != Filename){
if(NULL == (fIO = fopen(Filename,"wb"))){
printf("Unable to open file [%s]\n", Filename);
HelpOut();
return(1);
}
}
break;
case Work_call: if((Address & 3)){
printf("Bad call parametrs\n");
HelpOut();
return(1);
}
if(NULL != Filename){
if(NULL == (fIO = fopen(Filename,"wb"))){
printf("Unable to open file [%s]\n", Filename);
HelpOut();
return(1);
}
}
break;
case Work_load: if(NULL == Filename){
printf("No plugin name\n");
HelpOut();
return(1);
}
if(NULL == (fIO = fopen(Filename,"rb"))){
printf("Unable to open plugin file [%s]\n", Filename);
HelpOut();
return(1);
}
break;
case Work_write: if((Address & 3)){
printf("Bad write address\n");
HelpOut();
return(1);
}
if(NULL != Filename){
if(NULL == (fIO = fopen(Filename,"rb"))){
printf("Unable to open binary file [%s]\n", Filename);
HelpOut();
return(1);
}
}
break;
}
if(NULL == he){
if(FindRigol((struct sockaddr *)&SAddr)){
printf("Unable to autodetect IP-address\r\n");
return(1);
}
}
Sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(Sock == INVALID_SOCKET ){
printf("Unable to create socket, errror %d\r\n", WSAGetLastError());
return(1);
}
SAddr.sin_port = htons(6000u);
SAddr.sin_family = AF_INET;
if(NULL != he){
memcpy(&SAddr.sin_addr, he->h_addr_list[0], sizeof(SAddr.sin_addr));
}
if(SOCKET_ERROR == connect(Sock, (struct sockaddr *)&SAddr, 16)){
printf("connect error %d\r\n", WSAGetLastError());
return(3);
}
switch(Wm){
case Work_view:
if(0 < (Len = RigolGetIdent(Sock, Answer, sizeof(Answer)))){
printf("Device ID: [%s]\n", Answer);
}
else {
if(!SilentFlg) printf("Io Error, %d\n", Len);
RetCode = 10;
}
break;
case Work_read:
if(Length > 100000){
LongOperation = TRUE;
// if(!SilentFlg) printf("\n", Address);
}
ItemsCounter = 0;
while(Length > 0){
unsigned int CurLen;
CurLen = sizeof(Answer) / 4;
if(CurLen > Length)CurLen = Length;
if((ItemsCounter <=0) && LongOperation){
ItemsCounter = 100000;
if(!SilentFlg) printf("\r%08.8X ", Address);
}
if(0 > (Len = RigolReadDword(Sock, Address, CurLen, Answer))){
if(!SilentFlg) printf("Io Error, %d\n", Len);
RetCode = 10;
break;
}
if(NULL != fIO){
fwrite(Answer, 1, CurLen * 4, fIO);
}
else{
// out to stdout - to doooooooooo.....
}
ItemsCounter -=CurLen;
Address += CurLen*4;
Length -= CurLen;
}
break;
case Work_call:
if(0 < (Len = RigolCallFunc(Sock, Address, Param1, (unsigned short)Param2, (void*)&Answer))){
if(NULL != fIO) fwrite(Answer, 1, 4, fIO);
else printf("Result=0x%08.8X\n", *(unsigned long *)Answer);
}
else {
if(!SilentFlg) printf("Io Error, %d\n", Len);
RetCode = 10;
}
break;
//
case Work_load: if(0>= (Length = GetFSize(fIO))){
if(!SilentFlg) printf("Bad plugin size\n");
RetCode = 10;
break;
}
//
Length = (Length + 3) & (~3);
// Alloc Memory
Address = 0;
if(0 >= (Len = RigolCallFunc(Sock, 0x40026F84, Length, 0, &Address))){
if(!SilentFlg) printf("unable to call malloc, %d\n", Len);
RetCode = 10;
break;
}
if(0 == Address){
if(!SilentFlg) printf("unable to alloc %d bytes\n", Length);
RetCode = 20;
break;
}
else if(!SilentFlg) printf("Allocate %u bytes at %08.8X\n", Length, Address);
// Load To Address
if(0 >= (Len= LoadFile2Scope(Sock, fIO, Address, Length))){
if(!SilentFlg) printf("Load File Error, %d\n", Len);
RetCode = 20;
break;
}
// Call Plugin
if(0 >= (Len = RigolCallFunc(Sock, Address, 0,0, &Address))){
if(!SilentFlg) printf("Answer Error, %d\n", Len);
RetCode = 10;
break;
}
break;
case Work_write:if(NULL != fIO){
if(0>= (Length = GetFSize(fIO))){
if(!SilentFlg) printf("Bad image size\n");
RetCode = 10;
break;
}
Len = LoadFile2Scope(Sock, fIO, Address, Length);
}
else Len = RigolWriteDword(Sock, Address, Value);
if(0 >= Len){
if(!SilentFlg) printf("Write data Error, %d\n", Len);
RetCode = 20;
break;
}
break;
}
if(NULL != fIO) fclose(fIO);
closesocket(Sock);
return(RetCode);
}