-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.c
1102 lines (934 loc) · 35 KB
/
main.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
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include "ps2db0113.h"
#include "../dbms.h"
#include "ident.h"
static void ClearInput(void)
{
while (getchar() != '\n') {};
}
static unsigned int GetWord(int base)
{
char buffer[256], *ptr;
fgets(buffer, sizeof(buffer), stdin);
while ((ptr = strpbrk(buffer, "\n\r")) != NULL)
*ptr = '\0';
return strtoul(buffer, NULL, base);
}
static void GetString(char *buffer, int len)
{
char *ptr;
fgets(buffer, len, stdin);
while ((ptr = strpbrk(buffer, "\n\r")) != NULL)
*ptr = '\0';
}
static void AddMainboardModel(void)
{
unsigned short int HalfValue;
unsigned int value;
struct PS2IDBMainboardEntry NewEntry;
int result;
memset(&NewEntry, 0, sizeof(NewEntry));
printf("\nBoard information section\n");
printf("Status: ");
NewEntry.status = GetWord(0);
printf("Model name: ");
GetString(NewEntry.ModelName, sizeof(NewEntry.ModelName));
printf("Contributor's name: ");
GetString(NewEntry.ContributorName, sizeof(NewEntry.ContributorName));
printf("Model ID: ");
value = GetWord(16);
NewEntry.ModelID[0] = value & 0xFF;
NewEntry.ModelID[1] = (value >> 8) & 0xFF;
NewEntry.ModelID[2] = (value >> 16) & 0xFF;
printf("Console Model ID: ");
value = GetWord(16);
NewEntry.ConModelID[0] = value & 0xFF;
NewEntry.ConModelID[1] = (value >> 8) & 0xFF;
printf("EMCS ID: ");
value = GetWord(16);
NewEntry.EMCSID = value;
printf("Mainboard name: ");
GetString(NewEntry.MainboardName, sizeof(NewEntry.MainboardName));
printf("ROMVER: ");
GetString(NewEntry.romver, sizeof(NewEntry.romver));
printf("MECHACON region: ");
do
{
HalfValue = GetWord(0);
} while (HalfValue < 0 || HalfValue >= PS2IDBMS_GetNumDatabaseRecords(PS2IDB_COMPONENT_MG_REGION));
NewEntry.MECHACONVersion[0] = (u8)HalfValue;
printf("MECHACON major: ");
NewEntry.MECHACONVersion[1] = GetWord(0);
printf("MECHACON minor: ");
NewEntry.MECHACONVersion[2] = GetWord(0);
printf("MECHACON console type: ");
NewEntry.MECHACONVersion[3] = GetWord(0);
printf("ADD0x010: ");
NewEntry.ADD010 = GetWord(0);
printf("MECHACON renewal year (since 2000): ");
NewEntry.MRenewalDate[0] = GetWord(0);
printf("MECHACON renewal month: ");
NewEntry.MRenewalDate[1] = GetWord(0);
printf("MECHACON renewal date: ");
NewEntry.MRenewalDate[2] = GetWord(0);
printf("MECHACON renewal hour: ");
NewEntry.MRenewalDate[3] = GetWord(0);
printf("MECHACON renewal minute: ");
NewEntry.MRenewalDate[4] = GetWord(0);
printf("BoardInf: ");
NewEntry.BoardInf = GetWord(0);
printf("ROMGEN year (YYYY): ");
while ((NewEntry.ROMGEN_Year = GetWord(16)) == 0) {};
printf("ROMGEN month & date (MMDD): ");
while ((NewEntry.ROMGEN_MonthDate = GetWord(16)) == 0) {};
printf("Machine type: ");
NewEntry.MachineType = GetWord(0);
printf("\nSPU2 section\n");
printf("SPU2 revision: ");
while ((NewEntry.spu2.revision = GetWord(0)) == 0) {};
printf("\nUSB section\n");
printf("USB HC revision: ");
while ((NewEntry.usb.HcRevision = GetWord(0)) == 0) {};
printf("\ni.Link section\n");
printf("iLink ports: ");
NewEntry.iLink.NumPorts = GetWord(0);
printf("iLink maximum speed (0=S100, 1=S200, 2=S400): ");
NewEntry.iLink.MaxSpeed = GetWord(0);
printf("iLink compliance level: ");
NewEntry.iLink.ComplianceLevel = GetWord(0);
printf("iLink vendor ID: ");
NewEntry.iLink.VendorID = GetWord(0);
printf("iLink product ID: ");
NewEntry.iLink.ProductID = GetWord(0);
printf("\nDEV9 section\n");
NewEntry.ssbus.status = 0;
printf("SSBUS I/F revision: ");
while ((NewEntry.ssbus.revision = GetWord(0)) == 0) {};
printf("Does it have an AIF controller? [y/n] ");
do
{
result = getchar();
} while (result != 'n' && result != 'y');
// getchar();
if (result == 'y')
{
NewEntry.ssbus.status |= PS2DB_SSBUS_HAS_AIF;
printf("AIF controller revision: ");
while ((NewEntry.ssbus.AIFRevision = GetWord(0)) == 0) {};
}
else
{
NewEntry.ssbus.AIFRevision = 0;
}
printf("Does it have an integrated SPEED device? [y/n] ");
do
{
result = getchar();
} while (result != 'n' && result != 'y');
// getchar();
if (result == 'y')
{
NewEntry.ssbus.status |= PS2DB_SSBUS_HAS_SPEED;
printf("SPEED rev1 (revision): ");
while ((NewEntry.ssbus.SPEED.rev1 = GetWord(0)) == 0) {};
printf("SPEED rev3 (capabilities): ");
while ((NewEntry.ssbus.SPEED.rev3 = GetWord(0)) == 0) {};
printf("SPEED rev8: ");
while ((NewEntry.ssbus.SPEED.rev8 = GetWord(0)) == 0) {};
printf("SPEED PHY OUI: ");
while ((NewEntry.ssbus.SPEED.SMAP_PHY_OUI = GetWord(0)) == 0) {};
printf("SPEED PHY model: ");
while ((NewEntry.ssbus.SPEED.SMAP_PHY_VMDL = GetWord(0)) == 0) {};
printf("SPEED PHY revision: ");
while ((NewEntry.ssbus.SPEED.SMAP_PHY_REV = GetWord(0)) == 0) {};
}
printf("\nGS section\n");
printf("GS revision: ");
while ((NewEntry.gs.revision = GetWord(0)) == 0) {};
printf("GS ID: ");
while ((NewEntry.gs.id = GetWord(16)) == 0) {};
printf("\nIOP section\n");
printf("IOP revision: ");
while ((NewEntry.iop.revision = GetWord(0)) == 0) {};
printf("IOP RAM size (bytes): ");
while ((NewEntry.iop.RAMSize = GetWord(0)) == 0) {};
printf("\nEE section\n");
printf("EE implementation: ");
while ((NewEntry.ee.implementation = GetWord(0)) == 0) {};
printf("EE revision: ");
while ((NewEntry.ee.revision = GetWord(0)) == 0) {};
printf("EE FPU implementation: ");
while ((NewEntry.ee.FPUImplementation = GetWord(0)) == 0) {};
printf("EE FPU revision: ");
while ((NewEntry.ee.FPURevision = GetWord(0)) == 0) {};
printf("MPU Board ID: ");
while ((NewEntry.MPUBoardID = GetWord(16)) == 0) {};
printf("EE ICache size (2^(12+IC)): ");
while ((NewEntry.ee.ICacheSize = GetWord(0)) == 0) {};
printf("EE DCache size (2^(12+DC)): ");
while ((NewEntry.ee.DCacheSize = GetWord(0)) == 0) {};
printf("EE RAM size (bytes): ");
while ((NewEntry.ee.RAMSize = GetWord(0)) == 0) {};
printf("\nROM section\n");
NewEntry.BOOT_ROM.IsExists = 1;
printf("Boot ROM address: ");
while ((NewEntry.BOOT_ROM.StartAddress = GetWord(0)) == 0) {};
printf("Boot ROM size (bytes): ");
while ((NewEntry.BOOT_ROM.size = GetWord(0)) == 0) {};
printf("BOOT ROM CRC16: ");
NewEntry.BOOT_ROM.crc16 = GetWord(16);
printf("Does it have a DVD ROM chip? [y/n] ");
do
{
result = getchar();
} while (result != 'n' && result != 'y');
// getchar();
if (result == 'y')
{
NewEntry.DVD_ROM.IsExists = 1;
printf("DVD ROM address: ");
while ((NewEntry.DVD_ROM.StartAddress = GetWord(0)) == 0) {};
printf("DVD ROM size: ");
while ((NewEntry.DVD_ROM.size = GetWord(0)) == 0) {};
printf("DVD ROM CRC16: ");
while ((NewEntry.DVD_ROM.crc16 = GetWord(16)) == 0) {};
}
printf("Is the console modded? [y,n] ");
do
{
result = getchar();
} while (result != 'n' && result != 'y');
// getchar();
if (result == 'y')
NewEntry.status |= PS2IDB_STAT_ERR_MODDED;
printf("\nEnd of data entry.\n"
"Adding entry...");
if ((result = PS2IDBMS_AddMainboardModel(&NewEntry)) == 0)
{
printf("done!\n");
}
else
{
printf("failed!\n");
}
}
static void DisplayMainboardList(void)
{
unsigned int i, NumEntriesInDatabase, id;
const struct PS2IDBMainboardEntry *entry;
if ((NumEntriesInDatabase = PS2IDBMS_GetNumDatabaseRecords(PS2IDB_COMPONENT_MAINBOARD)) > 0)
{
printf("Model list (%u records):\n", NumEntriesInDatabase);
for (i = 0; i < NumEntriesInDatabase; i++)
{
entry = PS2IDBMS_GetMainboardDatabaseRecord(i);
printf("\t%03u. %s | %s\n", i + 1, entry->ModelName, entry->MainboardName);
}
printf("Model? (enter 0 to quit) ");
while ((id = GetWord(0)) != 0)
{
if (id > 0 && id <= NumEntriesInDatabase)
{
WriteSystemInformation(stdout, PS2IDBMS_GetMainboardDatabaseRecord(id - 1));
}
else
{
printf("Invalid ID entered.\n");
}
printf("ID? (enter 0 to quit) ");
}
}
else
{
printf("No entries in database.\n");
}
}
static void GenerateMainboardCSV(void)
{
unsigned int i, NumEntriesInDatabase, modelID;
unsigned short int conModelID;
const struct PS2IDBMainboardEntry *entry;
char FileName[128];
FILE *file;
if ((NumEntriesInDatabase = PS2IDBMS_GetNumDatabaseRecords(PS2IDB_COMPONENT_MAINBOARD)) > 0)
{
printf("File to save to (leave blank to cancel): ");
fgets(FileName, sizeof(FileName), stdin);
FileName[strlen(FileName) - 1] = '\0';
if (strlen(FileName) > 0)
{
if ((file = fopen(FileName, "w")) != NULL)
{
printf("Generating CSV...");
fprintf(file, "Model;Model ID;Console Model ID;EMCS ID;ROMVER;ROMGEN;Type;Region;MECHACON;M Renewal Date;Mainboard;ADD0x010;Chassis;Machine type;BoardInf"
";SPU2;Boot ROM;Boot ROM CRC16;DVD ROM;DVD ROM CRC16;USB;GS ID; GS revision;SSBUS I/F;AIF controller;SPEED revision;SPEED capabilities;"
"PHY OUI;PHY model;PHY revision;i.Link ports;i.Link maximum speed;i.Link compliance level;"
"i.Link vendor ID;i.Link product ID;EE implementation;EE revision;FPU implementation;"
"FPU revision;MPU Board ID;EE ICache size;EE DCache size;EE RAM size;IOP revision;IOP RAM size;Contributor;Remarks\n");
for (i = 0; i < NumEntriesInDatabase; i++)
{
entry = PS2IDBMS_GetMainboardDatabaseRecord(i);
modelID = entry->ModelID[0] | entry->ModelID[1] << 8 | entry->ModelID[2] << 16;
conModelID = entry->ConModelID[0] | entry->ConModelID[1] << 8;
fprintf(file, "\"%s\";\"0x%06x | %s\";0x%04x;\"0x%02x | %s\";\"%s\""
";%04x-%04x;\"0x%02x | %s\";\"0x%02x | %s\""
";\"v%u.%02u | %s\""
";",
entry->ModelName, modelID, GetModelIDDesc(modelID), conModelID, entry->EMCSID, GetEMCSIDDesc(entry->EMCSID), entry->romver,
entry->ROMGEN_MonthDate, entry->ROMGEN_Year,
entry->MECHACONVersion[3], GetSystemTypeDesc(entry->MECHACONVersion[3]), entry->MECHACONVersion[0], GetRegionDesc(entry->MECHACONVersion[0]),
entry->MECHACONVersion[1], entry->MECHACONVersion[2], GetMECHACONChipDesc((unsigned int)(entry->MECHACONVersion[1]) << 8 | entry->MECHACONVersion[2]));
if (entry->MECHACONVersion[1] < 5 || (entry->status & PS2IDB_STAT_ERR_MRENEWDATE))
fprintf(file, "\"----/--/-- --:--\"");
else
fprintf(file, "\"20%02x/%02x/%02x %02x:%02x\"", entry->MRenewalDate[0], entry->MRenewalDate[1], entry->MRenewalDate[2], entry->MRenewalDate[3], entry->MRenewalDate[4]);
fprintf(file, ";\"0x%04x | %s\";\"%s\";\"%s\""
";0x%08x;\"0x%02x | %s\";\"0x%02x | %s\""
";0x%08x | %uMbit; 0x%04x;",
entry->ADD010, GetADD010Desc(entry->ADD010), entry->MainboardName, GetChassisDesc(entry),
entry->MachineType, entry->BoardInf, GetMRPDesc(entry->BoardInf), entry->spu2.revision, GetSPU2ChipDesc(entry->spu2.revision),
entry->BOOT_ROM.StartAddress, entry->BOOT_ROM.size * 8 / 1024 / 1024, entry->BOOT_ROM.crc16);
if (entry->DVD_ROM.IsExists)
{
fprintf(file, "0x%08x | %uMbit; 0x%04x;", entry->DVD_ROM.StartAddress, entry->DVD_ROM.size * 8 / 1024 / 1024, entry->DVD_ROM.crc16);
}
else
fprintf(file, "-;-;");
fprintf(file, "%u.%u;0x%02x;\"%u.%02u | %s\";\"0x%02x | %s\";",
(entry->usb.HcRevision & 0xF0) >> 4, entry->usb.HcRevision & 0xF, entry->gs.id,
(entry->gs.revision & 0xF0) >> 4, entry->gs.revision & 0xF, GetGSChipDesc((unsigned short int)(entry->gs.id) << 8 | entry->gs.revision),
entry->ssbus.revision, GetSSBUSIFDesc(entry->ssbus.revision));
if (entry->ssbus.status & PS2DB_SSBUS_HAS_AIF)
{
fprintf(file, "%u;", entry->ssbus.AIFRevision);
}
else
{
fprintf(file, "-;");
}
if (entry->ssbus.status & PS2DB_SSBUS_HAS_SPEED)
{
fprintf(file, "\"0x%04x | %s\";\"%04x.%04x | %s\";\"0x%06x | %s\";\"0x%02x | %s\";0x%02x;",
entry->ssbus.SPEED.rev1, GetSPEEDDesc(entry->ssbus.SPEED.rev1), entry->ssbus.SPEED.rev3, entry->ssbus.SPEED.rev8, GetSPEEDCapsDesc(entry->ssbus.SPEED.rev3),
entry->ssbus.SPEED.SMAP_PHY_OUI, GetPHYVendDesc(entry->ssbus.SPEED.SMAP_PHY_OUI),
entry->ssbus.SPEED.SMAP_PHY_VMDL, GetPHYModelDesc(entry->ssbus.SPEED.SMAP_PHY_OUI, entry->ssbus.SPEED.SMAP_PHY_VMDL), entry->ssbus.SPEED.SMAP_PHY_REV);
}
else
fprintf(file, "-;-;-;-;-;");
fprintf(file, "%u;\"%u | %s\";\"0x%02x | %s\";\"0x%08x | %s\";0x%08x;0x%02x;\"%u.%02u | %s\";0x%02x;%u.%u;0x%04x;%uKB;%uKB;\"%u bytes\";\"%u.%02u | %s\";\"%u bytes\";\"%s\";",
entry->iLink.NumPorts, entry->iLink.MaxSpeed, GetiLinkSpeedDesc(entry->iLink.MaxSpeed), entry->iLink.ComplianceLevel, GetiLinkComplianceLvlDesc(entry->iLink.ComplianceLevel),
entry->iLink.VendorID, GetiLinkVendorDesc(entry->iLink.VendorID), entry->iLink.ProductID,
entry->ee.implementation, (entry->ee.revision & 0xF0) >> 4, entry->ee.revision & 0xF, GetEEChipDesc((unsigned short int)(entry->ee.implementation) << 8 | entry->ee.revision),
entry->ee.FPUImplementation, (entry->ee.FPURevision & 0xF0) >> 4, entry->ee.FPURevision & 0xF, entry->MPUBoardID,
CalculateCPUCacheSize(entry->ee.ICacheSize) / 1024, CalculateCPUCacheSize(entry->ee.DCacheSize) / 1024, entry->ee.RAMSize,
(entry->iop.revision & 0xF0) >> 4, entry->iop.revision & 0xF, GetIOPChipDesc(entry->iop.revision), entry->iop.RAMSize, entry->ContributorName);
if (entry->status)
{
int count = 0;
fprintf(file, "\"");
if (entry->status & PS2IDB_STAT_ERR_MVER)
{
fprintf(file, "Unable to get MECHACON version");
count++;
}
if (entry->status & PS2IDB_STAT_ERR_MNAME)
{
if (count > 0)
fprintf(file, ", ");
fprintf(file, "Unable to get model name");
count++;
}
if (entry->status & PS2IDB_STAT_ERR_MRENEWDATE)
{
if (count > 0)
fprintf(file, ", ");
fprintf(file, "Unable to get M renewal date");
count++;
}
if (entry->status & PS2IDB_STAT_ERR_ILINKID)
{
if (count > 0)
fprintf(file, ", ");
fprintf(file, "Unable to get i.Link ID");
count++;
}
if (entry->status & PS2IDB_STAT_ERR_CONSOLEID)
{
if (count > 0)
fprintf(file, ", ");
fprintf(file, "Unable to get console ID");
}
if (entry->status & PS2IDB_STAT_ERR_ADD010)
{
if (count > 0)
fprintf(file, ", ");
fprintf(file, "Unable to get ADD0x010");
}
if (entry->status & PS2IDB_STAT_ERR_MODDED)
{
if (count > 0)
fprintf(file, ", ");
fprintf(file, "Console is modded");
}
fprintf(file, "\"");
}
fprintf(file, "\n");
}
fclose(file);
printf("done!\n");
}
else
{
printf("Cannot create file: %s\n", FileName);
}
}
}
else
{
printf("No entries in database.\n");
}
}
static void UpdateModelEntry(void)
{
unsigned int id;
const struct PS2IDBMainboardEntry *entry;
struct PS2IDBMainboardEntry NewEntry;
int result;
printf("Enter the ID of the model entry to update (enter 0 to cancel): ");
if ((id = GetWord(0)) > 0)
{
if ((entry = PS2IDBMS_GetMainboardDatabaseRecord(id - 1)) != NULL)
{
printf("Selected record: %d. (%s) %s by %s\n\nNew mainboard name (leave blank to cancel): ", id, entry->ModelName, entry->MainboardName, entry->ContributorName);
memcpy(&NewEntry, entry, sizeof(struct PS2IDBMainboardEntry));
GetString(NewEntry.MainboardName, sizeof(NewEntry.MainboardName));
if (strlen(NewEntry.MainboardName) > 0)
{
printf("New contributor's name (leave blank to cancel): ");
GetString(NewEntry.ContributorName, sizeof(NewEntry.ContributorName));
if (strlen(NewEntry.ContributorName) > 0)
{
printf("Is the console modded? [y,n] ");
do
{
result = getchar();
} while (result != 'n' && result != 'y');
// getchar();
if (result == 'y')
NewEntry.status |= PS2IDB_STAT_ERR_MODDED;
else
NewEntry.status &= ~PS2IDB_STAT_ERR_MODDED;
printf("Updating...");
if ((result = PS2IDBMS_UpdateMainboardModel(id - 1, &NewEntry)) == 0)
{
printf("done!\n");
}
else
{
printf("failed!\n");
}
}
}
}
else
{
printf("Invalid ID entered.\n");
}
}
}
static void DeleteModelEntry(void)
{
unsigned int id;
const struct PS2IDBMainboardEntry *entry;
int result;
printf("Enter the ID of the model entry to delete (enter 0 to cancel): ");
if ((id = GetWord(0)) > 0)
{
if ((entry = PS2IDBMS_GetMainboardDatabaseRecord(id - 1)) != NULL)
{
printf("Selected record: %d. %s (%s)\n\nDelete? [y/n] ", id, entry->ModelName, entry->MainboardName);
if (getchar() == 'y')
{
printf("Deleting...");
if ((result = PS2IDBMS_DeleteMainboardRecord(id - 1)) == 0)
{
printf("done!\n");
}
else
{
printf("failed!\n");
}
}
ClearInput();
}
else
{
printf("Invalid ID entered.\n");
}
}
}
static const char *ComponentTypes[PS2IDB_COMPONENT_COUNT] = {
"Mainboard",
"EE",
"GS",
"IOP",
"SSBUS I/F",
"SPU2",
"MECHACON",
"SPEED",
"Ethernet PHY vendor",
"System type",
"Ethernet PHY model",
"MagicGate region",
"MRP board",
"Model ID",
"EMCS ID",
"ADD0x010"};
static void AddComponentEntry(int id)
{
struct PS2IDBComponentEntry NewEntry;
memset(&NewEntry, 0, sizeof(NewEntry));
printf("Enter component model name (text field). Leave blank to cancel: ");
GetString(NewEntry.name, sizeof(NewEntry.name));
if (strlen(NewEntry.name) > 0)
{
printf("Enter component revision (hex or dec number, example 0x00 or 0): ");
NewEntry.revision = GetWord(0);
if (PS2IDBMS_AddModel(id, &NewEntry) == EEXIST)
{
printf("Component already exists.\n");
}
}
}
static void ListComponentModels(int id)
{
unsigned int i, NumEntriesInDatabase;
const struct PS2IDBComponentEntry* entry;
if ((NumEntriesInDatabase = PS2IDBMS_GetNumDatabaseRecords(id)) > 0)
{
printf("Model list (%u records):\n", NumEntriesInDatabase);
for (i = 0; i < NumEntriesInDatabase; i++)
{
entry = PS2IDBMS_GetDatabaseRecord(id, i);
printf("\t%03u. 0x%04x | %s\n", i + 1, entry->revision, entry->name);
}
}
else
{
printf("No entries in database.\n");
}
}
static void SortComponentModels(int id)
{
unsigned int i, j;
const struct PS2IDBComponentEntry* entry_j;
const struct PS2IDBComponentEntry* entry_j1;
unsigned int NumEntriesInDatabase = PS2IDBMS_GetNumDatabaseRecords(id);
if (NumEntriesInDatabase > 1)
{
printf("Model list (%u records):\n", NumEntriesInDatabase);
for (i = 0; i < NumEntriesInDatabase; i++)
{
for (j = 0; j < NumEntriesInDatabase - 1; j++)
{
entry_j = PS2IDBMS_GetDatabaseRecord(id, j);
entry_j1 = PS2IDBMS_GetDatabaseRecord(id, j + 1);
if (entry_j->revision > entry_j1->revision)
{
struct PS2IDBComponentEntry EntryJ, EntryJ1;
memcpy(&EntryJ, entry_j, sizeof(struct PS2IDBComponentEntry));
memcpy(&EntryJ1, entry_j1, sizeof(struct PS2IDBComponentEntry));
PS2IDBMS_UpdateModel(id, j, &EntryJ1);
PS2IDBMS_UpdateModel(id, j + 1, &EntryJ);
}
}
}
printf("Sort complete.\n");
}
else
{
printf("Sort complete.\n");
}
}
static void UpdateComponentEntry(int id)
{
int index;
const struct PS2IDBComponentEntry *entry;
struct PS2IDBComponentEntry NewEntry;
printf("Enter id of component to update (enter 0 to cancel): ");
if ((index = GetWord(0)) > 0)
{
if ((entry = PS2IDBMS_GetDatabaseRecord(id, index - 1)) != NULL)
{
printf("Selected component is: %d. %s\t- 0x%04x\nNew model (leave blank to cancel): ", index, entry->name, entry->revision);
memcpy(&NewEntry, entry, sizeof(struct PS2IDBComponentEntry));
GetString(NewEntry.name, sizeof(NewEntry.name));
if (strlen(NewEntry.name) > 0)
PS2IDBMS_UpdateModel(id, index - 1, &NewEntry);
}
else
{
printf("Component does not exist.\n");
}
}
}
static void DeleteComponentEntry(int id)
{
int index;
const struct PS2IDBComponentEntry *entry;
printf("Enter id of component to delete (enter 0 to cancel): ");
if ((index = GetWord(0)) > 0)
{
if ((entry = PS2IDBMS_GetDatabaseRecord(id, index - 1)) != NULL)
{
printf("Selected component is: %d. %s\t- 0x%04x\nDelete? [y/n] ", index, entry->name, entry->revision);
if (getchar() == 'y')
{
PS2IDBMS_DeleteRecord(id, index - 1);
}
ClearInput();
}
else
{
printf("Component does not exist.\n");
}
}
}
static void AddComponent(void)
{
int i, choice;
printf("Select component type to add:\n");
for (i = 0; i < PS2IDB_COMPONENT_COUNT; i++)
printf("\t%02d. %s\n", i + 1, ComponentTypes[i]);
printf("Enter choice (Enter 0 to quit): ");
choice = GetWord(0) - 1;
if (choice >= 0 && choice < PS2IDB_COMPONENT_COUNT)
{
switch (choice)
{
case PS2IDB_COMPONENT_MAINBOARD:
AddMainboardModel();
break;
default:
AddComponentEntry(choice);
}
}
}
static void ListComponent(void)
{
int i, choice;
printf("Select component type to list:\n");
for (i = 0; i < PS2IDB_COMPONENT_COUNT; i++)
printf("\t%d. %s\n", i + 1, ComponentTypes[i]);
printf("Enter choice (Enter 0 to quit): ");
choice = GetWord(0) - 1;
if (choice >= 0 && choice < PS2IDB_COMPONENT_COUNT)
{
switch (choice)
{
case PS2IDB_COMPONENT_MAINBOARD:
DisplayMainboardList();
break;
default:
ListComponentModels(choice);
}
}
}
static void SortComponent(void)
{
int i, choice;
printf("Select component type to list:\n");
for (i = 0; i < PS2IDB_COMPONENT_COUNT; i++)
printf("\t%d. %s\n", i + 1, ComponentTypes[i]);
printf("Enter choice (Enter 0 to quit): ");
choice = GetWord(0) - 1;
if (choice >= 0 && choice < PS2IDB_COMPONENT_COUNT)
{
switch (choice)
{
case PS2IDB_COMPONENT_MAINBOARD:
printf("Mainboard list cannot be sorted.\n");
break;
default:
SortComponentModels(choice);
}
}
}
static void DeleteComponent(void)
{
int i, choice;
printf("Select component type to delete:\n");
for (i = 0; i < PS2IDB_COMPONENT_COUNT; i++)
printf("\t%d. %s\n", i + 1, ComponentTypes[i]);
printf("Enter choice (Enter 0 to quit): ");
choice = GetWord(0) - 1;
if (choice >= 0 && choice < PS2IDB_COMPONENT_COUNT)
{
switch (choice)
{
case PS2IDB_COMPONENT_MAINBOARD:
DeleteModelEntry();
break;
default:
DeleteComponentEntry(choice);
}
}
}
static void UpdateComponent(void)
{
int i, choice;
printf("Select component type to update:\n");
for (i = 0; i < PS2IDB_COMPONENT_COUNT; i++)
printf("\t%d. %s\n", i + 1, ComponentTypes[i]);
printf("Enter choice (Enter 0 to quit): ");
choice = GetWord(0) - 1;
if (choice >= 0 && choice < PS2IDB_COMPONENT_COUNT)
{
switch (choice)
{
case PS2IDB_COMPONENT_MAINBOARD:
UpdateModelEntry();
break;
default:
UpdateComponentEntry(choice);
}
}
}
static const struct PS2IDBMainboardEntry *_ImportMainboardModel(const char *filename, const char *ModelName, const char *ContributorName, short int isModded, int *pResult)
{
FILE *file;
int result, size;
struct PS2IDB_NewMainboardEntryHeader header;
struct PS2IDBMainboardEntry NewEntry;
const struct PS2IDBMainboardEntry *entry;
entry = NULL;
if ((file = fopen(filename, "rb")) != NULL)
{
fseek(file, 0, SEEK_END);
size = ftell(file);
rewind(file);
if (size == sizeof(struct PS2IDB_NewMainboardEntryHeader) + sizeof(struct PS2IDBMainboardEntry))
{
if (fread(&header, sizeof(struct PS2IDB_NewMainboardEntryHeader), 1, file) == 1)
{
if (header.magic[0] == '2' && header.magic[1] == 'N' && header.version == PS2IDB_NEWENT_FORMAT_VERSION)
{
if (fread(&NewEntry, sizeof(struct PS2IDBMainboardEntry), 1, file) == 1)
{
strncpy(NewEntry.MainboardName, ModelName, sizeof(NewEntry.MainboardName) - 1);
NewEntry.MainboardName[sizeof(NewEntry.MainboardName) - 1] = '\0';
strncpy(NewEntry.ContributorName, ContributorName, sizeof(NewEntry.ContributorName) - 1);
NewEntry.ContributorName[sizeof(NewEntry.ContributorName) - 1] = '\0';
if (isModded)
NewEntry.status |= PS2IDB_STAT_ERR_MODDED;
if ((result = PS2IDBMS_AddMainboardModel(&NewEntry)) == 0)
entry = &NewEntry;
else if (result == EEXIST)
entry = PS2IDBMS_LookupMainboardModel(&NewEntry);
}
else
result = EIO;
}
else
result = EINVAL;
}
else
result = EIO;
}
else
result = EINVAL;
fclose(file);
}
else
result = ENOENT;
*pResult = result;
return entry;
}
static int ImportMainboardModel(const char *filename, const char *ModelName, const char *ContributorName, short int isModded)
{
int result;
const struct PS2IDBMainboardEntry *entry;
printf("Importing %s...", filename);
entry = _ImportMainboardModel(filename, ModelName, ContributorName, isModded, &result);
switch (result)
{
case 0:
printf("done!\n");
break;
case ENOENT:
printf("file not found.\n");
break;
case EEXIST:
printf("already added.\nExisting model: %s - %s\n", entry->ModelName, entry->MainboardName);
break;
case EINVAL:
printf("invalid file format.\n");
break;
case EIO:
printf("I/O error.\n");
break;
default:
printf("failed!\n");
}
return result;
}
static void ImportMainboardModelPrompt(void)
{
char path[256];
char MainboardModel[16], ContributorName[16];
int result;
short int isModded;
printf("Filename of file to import (leave blank to cancel): ");
GetString(path, sizeof(path));
if (strlen(path) > 0)
{
printf("Mainboard model (leave blank to cancel): ");
GetString(MainboardModel, sizeof(MainboardModel));
if (strlen(MainboardModel))
{
printf("Contributor's name (leave blank to cancel): ");
GetString(ContributorName, sizeof(ContributorName));
if (strlen(ContributorName))
{
printf("Is the console modded? [y/n] ");
do
{
result = getchar();
} while (result != 'n' && result != 'y');
// getchar();
isModded = (result == 'y');
ImportMainboardModel(path, MainboardModel, ContributorName, isModded);
}
}
}
}
static void DisplaySyntaxHelp(void)
{
printf("Syntax: PS2IDBMS <Database file> <options>\n"
"\tOptions:\n"
"\t\t/add <model data file> <model name> <contributor's name> [/modded]\n"
"\nValues within [] are optional.\n");
}
int main(int argc, char *argv[])
{
unsigned char done;
int result, choice, i, j;
short int isModded;
printf("PlayStation 2 Ident model DBMS v1.06.1\n"
"--------------------------------------\n\n");
for (i = 2; i < argc; i++)
{
if (strcmp(argv[i], "/add") == 0 && i + 4 <= argc)
{
i++;
isModded = 0;
result = 0;
//Check for additional options.
for (j = i + 3; j < argc; j++)
{
if (!strcmp(argv[j], "/modded"))
{
isModded = 1;
}
else
{
//Unrecognized option.
result = -EINVAL;
break;
}