synctest.cpp
14 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
// synctest.cpp : Defines the entry point for the console application.
//
#include "StdAfx.h"
#include "bbcard.h"
#include <direct.h>
#include <errno.h>
#define BBC_BLOCK_SIZE 16*1024
#define TEST_COUNT 2
#define CMD_COUNT 13
#define GETOBJ_COUNT 5
#define RNDFILELEN(x) (((x)+(BBC_BLOCK_SIZE-1)) & ~(BBC_BLOCK_SIZE-1))
#define CID_ALL 0
#define CID_ANY 1
BBCHandle bh = -1;
char cmd[CMD_COUNT] = {'A',// test BBCCardPresent
'C', // test BBCStats
'N', // test BBCGetPlayerIdentity
'1', // test cert.sys
'2', // test private data
'3', // test crl.sys
'4', // test ticket.sys
'5', // test user data
'6', // test id.sys
'L', // test BBCGetContentList, BBCGetContent, BBCRemoveContent, BBCStoreContent
'V', // test BBCVerifySKSA
'E', // test BBCGetStateList, BBCGetState, BBCRenameState
'Q'}; // test BBCClose, BBCInit
struct objfunc_s {
char cmd;
char *name;
int (*getsize)(BBCHandle);
int (*getobj)(BBCHandle, void *, int);
int (*storeobj)(BBCHandle, const void *, int);
} objfunc[GETOBJ_COUNT] = {
{ '1', "Cert", BBCCertSize, BBCGetCert, BBCStoreCert },
{ '2', "PrivateData", BBCPrivateDataSize, BBCGetPrivateData, BBCStorePrivateData },
{ '3', "Crls", BBCCrlsSize, BBCGetCrls, BBCStoreCrls },
{ '4', "Tickets", BBCTicketsSize, BBCGetTickets, BBCStoreTickets },
{ '5', "UserData", BBCUserDataSize, BBCGetUserData, BBCStoreUserData },
};
_timeb t1, t2;
int deltat()
{
return (t2.time - t1.time)*1000 + (t2.millitm - t1.millitm);
}
float getKBs(int bytes, int ms)
{
if (ms == 0) ms = 1; // Make sure we are dividing by 1 msec at least
return bytes/1024.f/(ms/1000.f);
}
int process_B()
{
int rv;
printf(" Calling BBCInit...");
_ftime(&t1);
rv = BBCInit(BBC_SYNC);
_ftime(&t2);
printf(" returned %d (%ldms)\n", rv, deltat());
if (rv>=0) {
bh = rv;
/*
printf("\tread pipe: %s\n\twrite pipe: %s\n",
BBCGetDevName(bh, 0), BBCGetDevName(bh, 1));
*/
}
return rv;
}
int process_A()
{
int rv;
printf(" Calling BBCCardPresent...");
_ftime(&t1);
rv = BBCCardPresent(bh);
_ftime(&t2);
printf(" returned %d (%ldms)\n", rv, deltat());
return rv;
}
int process_C()
{
int rv, cardSize, sysSize, contentSize, freeSize;
printf(" Calling BBCStats2...");
_ftime(&t1);
rv = BBCStats2(bh, &cardSize, &sysSize, &contentSize, &freeSize);
_ftime(&t2);
printf(" returned %d with size %d: sysSize %d contentSize %d freeSize %d (%ldms)\n", rv, cardSize, sysSize, contentSize, freeSize, deltat());
return rv;
}
int process_N()
{
int rv, bbid = -1;
printf(" Calling BBCGetPlayerIdentity...\n");
_ftime(&t1);
rv = BBCGetPlayerIdentity(bh, &bbid);
_ftime(&t2);
printf(" returned %d with bbid %d (%ldms)\n", rv, bbid, deltat());
return rv;
}
int process_id(char* fname)
{
int bbid = -1;
char* buf;
int rv;
FILE* fp;
float rate;
buf = (char*)malloc(BBC_BLOCK_SIZE*sizeof(char));
printf(" Calling BBCGetIdentity...");
_ftime(&t1);
rv = BBCGetIdentity(bh, &bbid, buf, BBC_BLOCK_SIZE-sizeof(bbid));
_ftime(&t2);
if (rv>0 && fname!=NULL) {
fp = fopen(fname, "wb");
if (fp!=NULL) {
fwrite(&bbid, sizeof(bbid), 1, fp);
fwrite(buf, 1, rv, fp);
fclose(fp);
}
}
printf(" returned %d with bbid=0x%x (%ldms)\n", rv, bbid, deltat());
printf(" Calling BBCStoreIdentity...");
memset(buf, 0, BBC_BLOCK_SIZE);
fp = fopen(fname, "rb");
if (fp!=NULL) {
if (fread(buf, sizeof(char), BBC_BLOCK_SIZE, fp)>0) {
bbid = *(int*)buf;
_ftime(&t1);
rv = BBCStoreIdentity(bh, bbid,
buf+sizeof(bbid), BBC_BLOCK_SIZE-sizeof(bbid));
_ftime(&t2);
rate = getKBs(BBC_BLOCK_SIZE, deltat());
}
fclose(fp);
}
printf(" returned %d with bbid=0x%x (%ldms, %.1f KB/s)\n", rv, bbid, deltat(), rate);
free(buf);
return rv;
}
int process_obj(char cmd, char* fname)
{
int i, rv, len;
char* buf;
FILE* fp;
struct stat sb;
float rate;
for (i=0; i<GETOBJ_COUNT; i++)
if (cmd==objfunc[i].cmd) break;
printf(" Calling BBC%sSize...", objfunc[i].name);
_ftime(&t1);
len = objfunc[i].getsize(bh);
_ftime(&t2);
printf(" returned %d (%ldms)\n", len, deltat());
if (len>=0) {
buf = (char*)malloc(len);
printf(" Calling BBCGet%s...", objfunc[i].name);
_ftime(&t1);
rv = objfunc[i].getobj(bh, buf, len);
_ftime(&t2);
rate = getKBs(len, deltat());
printf(" returned %d (%ldms %.1f KB/s)\n", rv, deltat(), rate);
if (rv>0) {
if ( (fp=fopen(fname, "wb"))!=NULL ) {
fwrite(buf, 1, rv, fp);
fclose(fp);
}
stat(fname, &sb);
len = RNDFILELEN(sb.st_size);
free(buf);
buf = (char*) malloc(len);
memset(buf, 0, len);
if ( (fp=fopen(fname, "rb"))!=NULL ) {
fread(buf, 1, sb.st_size, fp);
fclose(fp);
printf(" Calling BBCStore%s...", objfunc[i].name);
_ftime(&t1);
rv = objfunc[i].storeobj(bh, buf, len);
_ftime(&t2);
rate = getKBs(len, deltat());
printf(" returned %d (%ldms %.1f KB/s)\n", rv, deltat(), rate);
}
free(buf);
} else
free(buf);
}
else rv = 0;
return rv;
}
int process_content(int cid)
{
int len, type, rv;
char* buf, fname[32];
FILE* fp;
struct stat sb;
float rate;
printf(" Calling BBCContentSize on %08x...", cid);
_ftime(&t1);
len = BBCContentSize(bh, cid, &type);
_ftime(&t2);
printf(" returned %d (%ldms)\n", len, deltat());
if (len>=0) {
buf = (char*)malloc(len);
printf(" Calling BBCGetContent...");
_ftime(&t1);
rv = BBCGetContent(bh, cid, buf, len, &type);
_ftime(&t2);
rate = getKBs(len, deltat());
printf(" returned %d with %08x.%s (%ldms %.1f KB/s)\n", rv, cid,
(type==BBC_CONTENT_APP) ? "app" :
((type==BBC_CONTENT_REC) ? "rec" : ""), deltat(), rate);
if (rv>0) {
if (type==BBC_CONTENT_APP)
sprintf(fname, "%08x.app", cid);
else if (type==BBC_CONTENT_REC)
sprintf(fname, "%08x.rec", cid);
if ( (fp=fopen(fname, "wb"))!=NULL ) {
fwrite(buf, 1, rv, fp);
fclose(fp);
}
if (type==BBC_CONTENT_APP) {
printf(" Calling BBCRemoveContent %08x.app...", cid);
_ftime(&t1);
rv = BBCRemoveContent(bh, cid);
_ftime(&t2);
printf(" returned %d (%ldms)\n", rv, deltat());
stat(fname, &sb);
len = RNDFILELEN(sb.st_size);
free(buf);
buf = (char*) malloc(len);
memset(buf, 0, len);
if ( (fp=fopen(fname, "rb"))!=NULL ) {
fread(buf, 1, sb.st_size, fp);
fclose(fp);
printf(" Calling BBCStoreContent %08x.app...", cid);
_ftime(&t1);
rv = BBCStoreContent(bh, cid, buf, len);
_ftime(&t2);
rate = getKBs(len, deltat());
printf(" returned %d (%ldms %.1f KB/s)\n", rv, deltat(), rate);
}
free(buf);
}
} else
free(buf);
}
return rv;
}
int process_L(int pick_cid)
{
int rv, i, cnt, app_cnt=0, type;
ContentId *cidlist, *apps;
ContentId cid;
int* cidsize;
printf(" Calling BBCContentListSize...");
_ftime(&t1);
rv = BBCContentListSize(bh);
_ftime(&t2);
printf(" returned %d (%ldms)\n", rv, deltat());
if (rv>0) {
cidlist = (ContentId*) malloc (rv*sizeof(ContentId));
cidsize = (int*) malloc(rv*sizeof(int));
apps = (ContentId*) malloc (rv*sizeof(ContentId));
printf(" Calling BBCGetContentList...");
_ftime(&t1);
rv = BBCGetContentList(bh, cidlist, cidsize, rv);
_ftime(&t2);
printf(" returned %d (%ldms)\n", rv, deltat());
cnt = rv;
for (i = 0; i < cnt; i++) {
rv = BBCContentSize(bh, cidlist[i], &type);
printf(" cid %x%s, size %d\n",
cidlist[i], type==BBC_CONTENT_APP ? ".app" : ".rec", cidsize[i]);
if (type==BBC_CONTENT_APP) {
apps[app_cnt] = cidlist[i];
app_cnt ++;
}
}
if (app_cnt>0) {
if (pick_cid == CID_ALL) {
for (i = 0; i < cnt; i++) {
process_content(apps[i]);
}
}
else if (pick_cid == CID_ANY) {
cid = (int) (((float)rand())/RAND_MAX*app_cnt);
process_content(apps[cid]);
}
}
free(cidlist);
free(cidsize);
free(apps);
}
return rv;
}
int process_state(int cid)
{
int len, rv, dirty;
char* buf, fname[32];
FILE* fp;
float rate;
EccSig sig;
printf(" Calling BBCStateSize on %08x.sta...", cid);
_ftime(&t1);
len = BBCStateSize(bh, cid);
_ftime(&t2);
printf(" returned %d (%ldms)\n", len, deltat());
if (len>0) {
buf = (char*) malloc(len*sizeof(char));
printf(" Calling BBCGetState...");
_ftime(&t1);
rv = BBCGetState(bh, cid, buf, len, sig, &dirty);
_ftime(&t2);
rate = getKBs(len, deltat());
printf(" returns %d (%ldms %.1f KB/s)\n", rv, deltat(), rate);
if (rv>=0) {
printf(" sig = %08x %08x ... %08x, dirty = %d\n", sig[0], sig[1], sig[15], dirty);
sprintf(fname, "%08x.sta", cid);
if ( (fp=fopen(fname, "wb"))!=NULL ) {
fwrite(buf, 1, len, fp);
fclose(fp);
}
}
free(buf);
}
return rv;
}
int process_E(int pick_cid)
{
int rv, i;
ContentId *cidlist;
ContentId cid;
printf(" Calling BBCStateListSize...");
_ftime(&t1);
rv = BBCStateListSize(bh);
_ftime(&t2);
printf(" returned %d (%ldms)\n", rv, deltat());
if (rv>0) {
cidlist = (ContentId*) malloc (rv*sizeof(ContentId));
printf(" Calling BBCGetStateList...");
_ftime(&t1);
rv = BBCGetStateList(bh, cidlist, rv);
_ftime(&t2);
printf(" returned %d (%ldms)\n", rv, deltat());
for (i = 0; i < rv; i++)
printf(" cid %x\n", cidlist[i]);
if (rv>0) {
if (pick_cid == CID_ALL) {
for (i = 0; i < rv; i++) {
process_state(cidlist[i]);
}
}
else if (pick_cid == CID_ANY) {
cid = (int) (((float)rand())/RAND_MAX*rv);
process_state(cidlist[cid]);
}
}
free(cidlist);
}
return rv;
}
int process_V()
{
printf(" Calling BBCVerifySKSA...\n");
// TODO: Call BBCVerifySKSA
return BBC_OK;
}
int process_Q()
{
int rv;
printf(" Calling BBCClose...");
rv = BBCClose(bh);
printf(" returned %d\n", rv);
return rv;
}
void gen_image(char* dir)
{
char curdir[256];
_getcwd(curdir, sizeof(curdir));
if (_mkdir(dir) == -1) {
if (errno != EEXIST) {
perror("Cannot create directory");
}
}
_chdir(dir);
if (process_B()<0) goto ret;
// TODO: get controler pak data, SKSA, .u0x files
process_A();
process_C();
process_N();
process_id("img_id.sys");
process_obj('1', "img_cert.sys");
process_obj('2', "img_pdata.sys");
process_obj('3', "img_crl.sys");
process_obj('4', "img_ticket.sys");
process_obj('5', "img_user.sys");
process_L(CID_ALL);
process_E(CID_ALL);
ret:
process_Q();
_chdir(curdir);
}
void sync_test(int count, bool stop)
{
int i, cidx, rv, bad = 0;
printf("\nrunning %d BBC commands\n", count);
if (process_B()<0) goto ret;
for (i=1; i<=count; i++) {
Sleep( (unsigned long) (((float)rand())/RAND_MAX*100.0) );
cidx = (int) (((float)rand())/RAND_MAX*CMD_COUNT);
printf("loop %d of %d: test %c:\n", i, count, cmd[cidx]);
switch (cmd[cidx]) {
case 'A': rv = process_A(); break;
case 'C': rv = process_C(); break;
case 'N': rv = process_N(); break;
case '1': rv = process_obj('1', "test_cert.sys"); break;
case '2': rv = process_obj('2', "test_pdata.sys"); break;
case '3': rv = process_obj('3', "test_crl.sys"); break;
case '4': rv = process_obj('4', "test_ticket.sys"); break;
case '5': rv = process_obj('5', "test_user.sys"); break;
case '6': rv = process_id("test_id.sys"); break;
case 'L': rv = process_L(CID_ANY); break;
case 'V': rv = process_V(); break;
case 'E': rv = process_E(CID_ANY); break;
case 'Q': /* Close and reinit */
rv = process_Q();
if (process_B()<0) goto ret;
break;
}
if (rv < 0) {
bad++;
if (stop) break;
}
}
if (i >= count) i = count;
printf("Looped %d of %d: %d ok, %d bad:\n", i, count, i-bad, bad);
ret:
process_Q();
}
void cont_test(int count, bool stop)
{
int i, rv, bad = 0;
printf("\nrunning %d BBC commands\n", count);
for (i=1; i<=count; i++) {
printf("loop %d of %d\n", i, count);
if ((rv = process_B()) >= 0) {
//rv = process_id("test_id.sys");
rv = process_L(CID_ANY);
if (rv >= 0)
rv = process_Q();
}
if (rv < 0) {
bad++;
if (stop) break;
}
Sleep( (unsigned long) (((float)rand())/RAND_MAX*100.0) );
}
if (i >= count) i = count;
printf("Looped %d of %d: %d ok, %d bad:\n", i, count, i-bad, bad);
}
void usage()
{
printf("Usage: synctest.exe -i <dir> -c <count> [-t <testnum>]\n");
}
int main(int argc, char* argv[])
{
int i, icount = 0, testnum = 0;
char* imagename = NULL;
bool stop = false;
if (argc<2) {
usage();
return -1;
}
srand( (unsigned)time( NULL ) );
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-i")) {
i++;
if (i >= argc || argv[i][0] == '-') {
printf("Missing argument to -i\n");
usage();
return -1;
}
imagename = argv[i];
}
else if (!strcmp(argv[i], "-s")) {
stop = true;
}
else if (!strcmp(argv[i], "-c")) {
i++;
if (i >= argc || argv[i][0] == '-') {
printf("Missing argument to -c\n");
usage();
return -1;
}
icount = atoi(argv[i]);
}
else if (!strcmp(argv[i], "-t")) {
i++;
if (i >= argc || argv[i][0] == '-') {
printf("Missing argument to -t\n");
usage();
return -1;
}
testnum = atoi(argv[i]);
if (testnum >= TEST_COUNT) {
printf("Invalid test number %d: testnum must be from 0 to %d\n",
testnum, TEST_COUNT);
return -1;
}
}
else {
printf("Unknown option %s\n", argv[i]);
usage();
return -1;
}
}
if (imagename) {
printf("generating golden image %s\n", imagename);
gen_image(imagename);
}
if (icount>0) {
switch (testnum) {
case 0:
sync_test(icount, stop);
break;
case 1:
cont_test(icount, stop);
break;
default:
printf("Invalid test number %d\n", testnum);
break;
}
}
return 0;
}