-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathXbe.cpp
1465 lines (1168 loc) · 47.2 KB
/
Xbe.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
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
// This file is part of Cxbe
// SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-FileCopyrightText: 2002-2003 Aaron Robinson <[email protected]>
// SPDX-FileCopyrightText: 2018 Lucas Jansson
// SPDX-FileCopyrightText: 2019-2021 Stefan Schmidt
// SPDX-FileCopyrightText: 2019 Jannik Vogel
#include "Xbe.h"
#include "Exe.h"
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <locale.h>
#include <memory.h>
#include <stdlib.h>
#include <time.h>
static const char kKernelImageName[] = "xboxkrnl.exe";
static uint32 CountNonKernelImportTableEntries(class Exe *x_Exe, uint32_t *extra_bytes);
static size_t BasenameOffset(const std::string &path)
{
size_t sep_offset = path.find_last_of("/\\");
if(sep_offset == std::string::npos)
{
return 0;
}
return sep_offset + 1;
}
// construct via Exe file object
Xbe::Xbe(class Exe *x_Exe, const char *x_szTitle, bool x_bRetail, const std::vector<uint08> *logo,
const char *x_szDebugPath)
{
ConstructorInit();
time_t CurrentTime;
time(&CurrentTime);
printf("Xbe::Xbe: Pass 1 (Simple Pass)...");
std::string debug_path = x_szDebugPath;
// pass 1
{
// standard Xbe magic number
m_Header.dwMagic = *(uint32 *)"XBEH";
// nobody has the private key yet, so zero this out
memset(m_Header.pbDigitalSignature, 0, 256);
// we'll only allow 0x00010000 for now
m_Header.dwBaseAddr = 0x00010000;
// this is a constant value
m_Header.dwSizeofImageHeader = sizeof(m_Header);
// we'll have the same number of sections as the Exe
m_Header.dwSections = x_Exe->m_Header.m_sections;
// TODO: allow configuration
{
memset(&m_Header.dwInitFlags, 0, sizeof(m_Header.dwInitFlags));
m_Header.dwInitFlags.bLimit64MB = true;
m_Header.dwInitFlags.bDontSetupHarddisk = false;
m_Header.dwInitFlags.bMountUtilityDrive = true;
}
// various PE copies
{
// StackCommit actually means StackReserve
m_Header.dwPeStackCommit = x_Exe->m_OptionalHeader.m_sizeof_stack_reserve;
m_Header.dwPeHeapReserve = x_Exe->m_OptionalHeader.m_sizeof_heap_reserve;
m_Header.dwPeHeapCommit = x_Exe->m_OptionalHeader.m_sizeof_heap_commit;
m_Header.dwPeSizeofImage = x_Exe->m_OptionalHeader.m_sizeof_image;
m_Header.dwPeChecksum = 0x00000000;
m_Header.dwPeTimeDate = x_Exe->m_Header.m_timedate;
}
// build time/date
m_Header.dwTimeDate = CurrentTime;
m_Header.dwNonKernelImportDirAddr = 0;
// Need one or more library version, otherwise VerifierX on XDKs crashes
m_Header.dwLibraryVersions = 1;
// TODO: generate these values
m_Header.dwKernelLibraryVersionAddr = 0;
m_Header.dwXAPILibraryVersionAddr = 0;
}
printf("OK\n");
printf("Xbe::Xbe: Pass 2 (Calculating Requirements)...");
// pass 2
uint32 non_kernel_import_table_bytes = 0;
{
// make-room cursor
uint32 mrc = m_Header.dwBaseAddr + sizeof(m_Header);
// make room for certificate
{
m_Header.dwCertificateAddr = mrc;
mrc += sizeof(m_Certificate);
}
// make room for section headers
{
m_Header.dwSectionHeadersAddr = mrc;
mrc += m_Header.dwSections * (sizeof(*m_SectionHeader));
// make room for head/tail reference count words
mrc += (m_Header.dwSections + 1) * 2;
// make room for section names
for(uint32 v = 0; v < m_Header.dwSections; v++)
{
uint32 s = 0;
while(s < 8 && x_Exe->m_SectionHeader[v].m_name[s] != '\0')
s++;
mrc += s + 1;
}
}
// make room for non-kernel imports and a null terminator
{
uint32 name_bytes = 0;
uint32 num_entries = CountNonKernelImportTableEntries(x_Exe, &name_bytes);
if(num_entries)
{
if(x_bRetail)
{
printf("Xbe::Xbe: Non-kernel imports detected in RETAIL mode. Ignoring...\n");
}
else
{
non_kernel_import_table_bytes = mrc;
mrc = RoundUp(mrc, 0x04);
m_Header.dwNonKernelImportDirAddr = mrc;
mrc += (1 + num_entries) * sizeof(XBE_IMAGE_IMPORT_DESCRIPTOR);
mrc = RoundUp(mrc, 0x04);
m_ImportNameAddr = mrc;
mrc += name_bytes;
mrc = RoundUp(mrc, 0x04);
non_kernel_import_table_bytes = mrc - non_kernel_import_table_bytes;
}
}
}
// make room for library versions
{
if(m_Header.dwLibraryVersions == 0)
{
m_Header.dwLibraryVersionsAddr = 0;
}
else
{
m_Header.dwLibraryVersionsAddr = mrc;
mrc += m_Header.dwLibraryVersions * static_cast<uint32>(sizeof(*m_LibraryVersion));
}
}
// make room for debug path / debug file names
{
uint32 path_bytes = debug_path.size() + 1;
size_t sep_offset = BasenameOffset(debug_path);
uint32 filename_bytes = path_bytes - sep_offset;
mrc = RoundUp(mrc, 0x04);
m_Header.dwDebugUnicodeFilenameAddr = mrc;
mrc = RoundUp(mrc + filename_bytes * 2, 0x04);
m_Header.dwDebugPathnameAddr = mrc;
m_Header.dwDebugFilenameAddr = m_Header.dwDebugPathnameAddr + sep_offset;
mrc += path_bytes;
}
// make room for largest possible logo bitmap
{
mrc = RoundUp(mrc, 0x10);
m_Header.dwLogoBitmapAddr = mrc;
if(!logo)
{
m_Header.dwSizeofLogoBitmap = defaultXbeLogoSize;
}
else
{
m_Header.dwSizeofLogoBitmap = logo->size();
}
mrc += m_Header.dwSizeofLogoBitmap;
}
// update size of headers
m_Header.dwSizeofHeaders = mrc - m_Header.dwBaseAddr;
}
printf("OK\n");
printf("Xbe::Xbe: Pass 3 (Generating Xbe)...\n");
// pass 3
{
m_Header.dwPeBaseAddr = m_Header.dwBaseAddr + RoundUp(m_Header.dwSizeofHeaders, 0x1000) -
x_Exe->m_SectionHeader[0].m_virtual_addr;
// encode entry point
{
printf("Xbe::Xbe: Encoding %s Entry Point...", x_bRetail ? "Retail" : "Debug");
uint32 ep = x_Exe->m_OptionalHeader.m_entry + m_Header.dwPeBaseAddr;
if(x_bRetail)
ep ^= XOR_EP_RETAIL;
else
ep ^= XOR_EP_DEBUG;
m_Header.dwEntryAddr = ep;
printf("OK (0x%.08X)\n", ep);
}
{
printf("Xbe::Xbe: Relocating TLS directory...");
uint32 tls_directory =
x_Exe->m_OptionalHeader.m_image_data_directory[IMAGE_DIRECTORY_ENTRY_TLS]
.m_virtual_addr;
if(!tls_directory)
m_Header.dwTLSAddr = 0;
else
m_Header.dwTLSAddr = tls_directory + m_Header.dwPeBaseAddr;
printf("OK (0x%.08X)\n", m_Header.dwTLSAddr);
}
// header write cursor
uint32 hwc = m_Header.dwBaseAddr + sizeof(m_Header);
// check if we need to store extra header bytes (we always will)
if(m_Header.dwSizeofHeaders > sizeof(m_Header))
{
printf("Xbe::Xbe: Found Extra Header Bytes...");
uint32 ExSize = RoundUp(m_Header.dwSizeofHeaders - sizeof(m_Header), 0x1000);
m_HeaderEx = new char[ExSize];
memset(m_HeaderEx, 0, ExSize);
printf("OK\n");
}
// start a write buffer inside of m_HeaderEx
char *szBuffer = m_HeaderEx;
// write certificate
{
// certificate size is a constant
m_Certificate.dwSize = sizeof(m_Certificate);
m_Certificate.dwTimeDate = CurrentTime;
// TODO: generate in the form CX-9999
m_Certificate.dwTitleId = 0xFFFF0002;
// title name
memset(m_Certificate.wszTitleName, 0, sizeof(m_Certificate.wszTitleName));
// mbstowcs(m_Certificate.wszTitleName, x_szTitle, 40);
const char *p = x_szTitle;
char *q = (char *)m_Certificate.wszTitleName;
while(*p != 0x00)
{
*q++ = *p++;
*q++ = 0x00;
}
// zero out alternate ids
{
for(uint32 c = 0; c < 0x10; c++)
m_Certificate.dwAlternateTitleId[c] = 0;
}
// for now we'll just allow any media you could want
m_Certificate.dwAllowedMedia =
XBEIMAGE_MEDIA_TYPE_HARD_DISK | XBEIMAGE_MEDIA_TYPE_DVD_CD |
XBEIMAGE_MEDIA_TYPE_MEDIA_BOARD | XBEIMAGE_MEDIA_TYPE_NONSECURE_HARD_DISK |
XBEIMAGE_MEDIA_TYPE_NONSECURE_MODE;
// TODO: allow configuration
m_Certificate.dwGameRegion = XBEIMAGE_GAME_REGION_MANUFACTURING |
XBEIMAGE_GAME_REGION_NA | XBEIMAGE_GAME_REGION_JAPAN |
XBEIMAGE_GAME_REGION_RESTOFWORLD;
// TODO: allow configuration
m_Certificate.dwGameRatings = 0xFFFFFFFF;
// always disk 0, AFAIK
m_Certificate.dwDiskNumber = 0;
// TODO: allow configuration
m_Certificate.dwVersion = 0;
// generate blank LAN, signature, and alternate signature keys
{
for(uint32 v = 0; v < 0x10; v++)
m_Certificate.bzLanKey[v] = m_Certificate.bzSignatureKey[v] = 0;
for(uint32 x = 0; x < 0x10; x++)
for(uint32 y = 0; y < 0x10; y++)
m_Certificate.bzTitleAlternateSignatureKey[x][y] = 0;
}
// write certificate
{
memcpy(szBuffer, &m_Certificate, sizeof(m_Certificate));
szBuffer += sizeof(m_Certificate);
hwc += sizeof(m_Certificate);
}
}
// generate ascii title from certificate title name
setlocale(LC_ALL, "English");
// wcstombs(m_szAsciiTitle, m_Certificate.wszTitleName, 40);
char *c = m_szAsciiTitle;
char *d = (char *)m_Certificate.wszTitleName;
while((uint16 *)d < &m_Certificate.wszTitleName[40] && *d)
{
*c++ = *d++;
d++;
}
*c = '\0';
// write section headers / section names
{
m_szSectionName = new char[m_Header.dwSections][9];
m_SectionHeader = new SectionHeader[m_Header.dwSections];
uint32 SectionCursor = RoundUp(m_Header.dwSizeofHeaders, 0x1000);
// head/tail reference count write buffer
uint16 *htrc = (uint16 *)(szBuffer + m_Header.dwSections * sizeof(*m_SectionHeader));
// section write buffer
char *secn = (char *)((uintptr_t)htrc + (m_Header.dwSections + 1) * 2);
// head/tail reference count write cursor
uint32 hwc_htrc = hwc + m_Header.dwSections * sizeof(*m_SectionHeader);
// section write cursor
uint32 hwc_secn = hwc_htrc + (m_Header.dwSections + 1) * 2;
printf("Xbe::Xbe: Generating Section Headers...\n");
for(uint32 v = 0; v < m_Header.dwSections; v++)
{
printf("Xbe::Xbe: Generating Section Header %.04X...", v);
uint32 characteristics = x_Exe->m_SectionHeader[v].m_characteristics;
memset(&m_SectionHeader[v].dwFlags, 0, sizeof(m_SectionHeader->dwFlags));
if(characteristics & IMAGE_SCN_MEM_WRITE)
m_SectionHeader[v].dwFlags.bWritable = true;
if((characteristics & IMAGE_SCN_MEM_EXECUTE) ||
(characteristics & IMAGE_SCN_CNT_CODE))
m_SectionHeader[v].dwFlags.bExecutable = true;
m_SectionHeader[v].dwFlags.bPreload = true;
m_SectionHeader[v].dwVirtualAddr =
x_Exe->m_SectionHeader[v].m_virtual_addr + m_Header.dwPeBaseAddr;
if(v < m_Header.dwSections - 1)
m_SectionHeader[v].dwVirtualSize =
x_Exe->m_SectionHeader[v + 1].m_virtual_addr -
x_Exe->m_SectionHeader[v].m_virtual_addr;
else
m_SectionHeader[v].dwVirtualSize =
RoundUp(x_Exe->m_SectionHeader[v].m_virtual_size, 4);
m_SectionHeader[v].dwRawAddr = SectionCursor;
// calculate sizeof_raw by locating the last non-zero value in the raw section data
{
uint32 r = x_Exe->m_SectionHeader[v].m_sizeof_raw;
if(r > 0)
{
r--;
while(r > 0)
{
if(x_Exe->m_bzSection[v][r--] != 0)
break;
}
}
// word aligned
m_SectionHeader[v].dwSizeofRaw = RoundUp(r + 2, 4);
}
SectionCursor += RoundUp(m_SectionHeader[v].dwSizeofRaw, 0x1000);
// head/tail reference count
{
m_SectionHeader[v].dwHeadSharedRefCountAddr = hwc_htrc;
htrc[v] = 0;
hwc_htrc += 2;
m_SectionHeader[v].dwTailSharedRefCountAddr = hwc_htrc;
htrc[v + 1] = 0;
}
// section name
{
uint32 s = 0;
memset(secn, 0, 8);
m_SectionHeader[v].dwSectionNameAddr = hwc_secn;
while(s < 8 && x_Exe->m_SectionHeader[v].m_name[s] != '\0')
{
m_szSectionName[v][s] = secn[s] = x_Exe->m_SectionHeader[v].m_name[s];
s++;
}
m_szSectionName[v][s] = '\0';
secn += s + 1;
hwc_secn += s + 1;
}
m_SectionHeader[v].dwSectionRefCount = 0;
// write section digest (just zeros)
memset(m_SectionHeader[v].bzSectionDigest, 0, 20);
// write section header
memcpy(szBuffer, &m_SectionHeader[v], sizeof(*m_SectionHeader));
szBuffer += sizeof(*m_SectionHeader);
printf("OK\n");
}
hwc = hwc_secn;
szBuffer = m_HeaderEx + hwc - (m_Header.dwBaseAddr + sizeof(m_Header));
}
// Reserve space for the non-kernel import table.
{
szBuffer += non_kernel_import_table_bytes;
hwc += non_kernel_import_table_bytes;
}
// Write (placeholder) library versions
{
m_LibraryVersion = new LibraryVersion[m_Header.dwLibraryVersions];
for(uint32 v = 0; v < m_Header.dwLibraryVersions; ++v)
{
char tmp[9];
snprintf(tmp, sizeof(tmp), "CXBE%d", v);
for(uint32 c = 0; c < 8; ++c)
{
m_LibraryVersion[v].szName[c] = tmp[c];
}
m_LibraryVersion[v].wMajorVersion = 0;
m_LibraryVersion[v].wMinorVersion = 0;
m_LibraryVersion[v].wBuildVersion = 0;
m_LibraryVersion[v].dwFlags.QFEVersion = 0;
m_LibraryVersion[v].dwFlags.Approved = 0;
m_LibraryVersion[v].dwFlags.bDebugBuild = 0;
// write library version
memcpy(szBuffer, &m_LibraryVersion[v], sizeof(*m_LibraryVersion));
szBuffer += sizeof(*m_LibraryVersion);
hwc += sizeof(*m_LibraryVersion);
}
}
// write debug path / debug file names
{
uint08 *debug_path_field = GetAddr(m_Header.dwDebugPathnameAddr);
uint32 path_size_with_terminator = debug_path.size() + 1;
memcpy(debug_path_field, debug_path.c_str(), path_size_with_terminator);
uint08 *unicode_filename = GetAddr(m_Header.dwDebugUnicodeFilenameAddr);
uint08 *filename = GetAddr(m_Header.dwDebugFilenameAddr);
do
{
*unicode_filename++ = *filename++;
*unicode_filename++ = 0;
} while(*filename);
}
{
printf("Xbe::Xbe: Generating Logo Bitmap...");
uint08 *RawAddr = GetAddr(m_Header.dwLogoBitmapAddr);
if(logo)
{
memcpy(RawAddr, logo->data(), logo->size());
printf("OK (custom)\n");
}
else
{
memcpy(RawAddr, defaultXbeLogo, defaultXbeLogoSize);
printf("OK (default)\n");
}
}
// write sections
{
printf("Xbe::Xbe: Generating Sections...\n");
m_bzSection = new uint08 *[m_Header.dwSections];
memset(m_bzSection, 0, m_Header.dwSections);
for(uint32 v = 0; v < m_Header.dwSections; v++)
{
printf("Xbe::Xbe: Generating Section %.04X...", v);
uint32 RawSize = m_SectionHeader[v].dwSizeofRaw;
uint32 VirtSize = m_SectionHeader[v].dwVirtualSize;
uint32 maxSize = std::max(VirtSize, RawSize);
m_bzSection[v] = new uint08[maxSize];
memset(m_bzSection[v], 0, maxSize);
memcpy(m_bzSection[v], x_Exe->m_bzSection[v], RawSize);
printf("OK\n");
}
}
// process kernel and debug thunk tables
if(!ProcessImportTable(x_Exe, x_bRetail))
{
goto cleanup;
}
}
printf("Xbe::Xbe: Pass 4 (Finalizing)...\n");
// pass 4
{
m_Header.dwSizeofImage = m_SectionHeader[m_Header.dwSections - 1].dwVirtualAddr +
m_SectionHeader[m_Header.dwSections - 1].dwVirtualSize -
m_Header.dwBaseAddr;
if(m_Header.dwSizeofImage >= 64 * 1024 * 1024)
{
fprintf(
stderr,
"Xbe::Xbe: Image size exceeds 64MiB - this xbe will not run on retail systems\n");
}
else if(m_Header.dwSizeofImage >= 32 * 1024 * 1024)
{
fprintf(stderr, "Xbe::Xbe: Image size exceeds 32MiB\n");
}
// relocate to base : 0x00010000
{
printf("Xbe::Xbe: Relocating to Base 0x00010000...");
uint32 fixCount = 0;
uint32 relo_addr = x_Exe->m_OptionalHeader.m_image_data_directory[5].m_virtual_addr;
uint32 relo_size = x_Exe->m_OptionalHeader.m_image_data_directory[5].m_size;
uint32 dwBaseDiff = m_Header.dwPeBaseAddr - x_Exe->m_OptionalHeader.m_image_base;
uint08 *reloc = GetAddr(relo_addr + m_Header.dwPeBaseAddr);
// relocate, if necessary
if(reloc != 0)
{
uint32 v = 0;
// relocate each relocation block
while(v < relo_size)
{
uint32 block_addr = *(uint32 *)&reloc[v + 0];
uint32 block_stop = *(uint32 *)&reloc[v + 4] + v;
v += 8;
// relocate each rva
while(v < block_stop && v < relo_size)
{
uint16 data = *(uint16 *)&reloc[v];
uint32 type = (data & 0xF000) >> 12;
if(type == 0)
{
v += 2;
break;
}
// 32-bit field relocation
if(type == IMAGE_REL_BASED_HIGHLOW)
{
fixCount++;
uint32 dwFixAddr = block_addr + (data & 0x0FFF) + m_Header.dwPeBaseAddr;
uint08 *bzModRVA = GetAddr(dwFixAddr);
if(bzModRVA != 0)
*(uint32 *)bzModRVA += dwBaseDiff;
}
else
{
SetError("Unsupported relocation type", true);
goto cleanup;
}
v += 2;
}
}
}
printf("OK (%d Fixups)\n", fixCount);
}
}
cleanup:
if(GetError() != 0)
{
printf("FAILED!\n");
printf("Xbe::Xbe: ERROR -> %s\n", GetError());
}
}
// deconstructor
Xbe::~Xbe()
{
if(m_bzSection != 0)
{
for(uint32 v = 0; v < m_Header.dwSections; v++)
delete[] m_bzSection[v];
delete[] m_bzSection;
}
delete m_XAPILibraryVersion;
delete m_KernelLibraryVersion;
delete[] m_LibraryVersion;
delete m_TLS;
delete[] m_szSectionName;
delete[] m_SectionHeader;
delete[] m_HeaderEx;
}
// export to Xbe file
void Xbe::Export(const char *x_szXbeFilename)
{
if(GetError() != 0)
return;
char szBuffer[260];
printf("Xbe::Export: Writing Xbe file...");
FILE *XbeFile = fopen(x_szXbeFilename, "wb");
// verify Xbe file was opened successfully
if(XbeFile == NULL)
{
SetError("Could not open Xbe file", true);
goto cleanup;
}
printf("OK\n");
// write Xbe image header
{
printf("Xbe::Export: Writing Image Header...");
if(fwrite(&m_Header, sizeof(m_Header), 1, XbeFile) != 1)
{
SetError("Unexpected write error while writing Xbe Image Header", false);
goto cleanup;
}
printf("OK\n");
printf("Xbe::Export: Writing Image Header Extra Bytes...");
if(fwrite(m_HeaderEx, m_Header.dwSizeofHeaders, 1, XbeFile) != 1)
{
SetError("Unexpected write error while writing Xbe Image Header (Ex)", false);
goto cleanup;
}
printf("OK\n");
}
// write Xbe certificate
{
printf("Xbe::Export: Writing Certificate...");
fseek(XbeFile, m_Header.dwCertificateAddr - m_Header.dwBaseAddr, SEEK_SET);
if(fwrite(&m_Certificate, sizeof(m_Certificate), 1, XbeFile) != 1)
{
SetError("Unexpected write error while writing Xbe Certificate", false);
goto cleanup;
}
printf("OK\n");
}
// write Xbe section headers
{
printf("Xbe::Export: Writing Section Headers...\n");
fseek(XbeFile, m_Header.dwSectionHeadersAddr - m_Header.dwBaseAddr, SEEK_SET);
for(uint32 v = 0; v < m_Header.dwSections; v++)
{
printf("Xbe::Export: Writing Section Header 0x%.04X...", v);
if(fwrite(&m_SectionHeader[v], sizeof(*m_SectionHeader), 1, XbeFile) != 1)
{
snprintf(szBuffer, sizeof(szBuffer),
"Unexpected write error while writing Xbe Section %d (%Xh)", v, v);
SetError(szBuffer, false);
goto cleanup;
}
printf("OK\n");
}
}
// write Xbe sections
{
printf("Xbe::Export: Writing Sections...\n");
for(uint32 v = 0; v < m_Header.dwSections; v++)
{
printf("Xbe::Export: Writing Section 0x%.04X (%s)...", v, m_szSectionName[v]);
uint32 RawSize = m_SectionHeader[v].dwSizeofRaw;
uint32 RawAddr = m_SectionHeader[v].dwRawAddr;
fseek(XbeFile, RawAddr, SEEK_SET);
if(RawSize == 0)
{
printf("OK\n");
continue;
}
if(fwrite(m_bzSection[v], RawSize, 1, XbeFile) != 1)
{
snprintf(szBuffer, sizeof(szBuffer),
"Unexpected write error while writing Xbe Section %d (%Xh) (%s)", v, v,
m_szSectionName[v]);
SetError(szBuffer, false);
goto cleanup;
}
printf("OK\n");
}
}
// zero pad
{
printf("Xbe::Export: Writing Zero Padding...");
fpos_t pos;
uint32 remaining = 0;
fgetpos(XbeFile, &pos);
remaining = (uint32)(0x1000 - ftell(XbeFile) % 0x1000);
// write remaining bytes
{
char *szBuffer = new char[remaining];
for(uint32 v = 0; v < remaining; v++)
szBuffer[v] = 0;
fwrite(szBuffer, remaining, 1, XbeFile);
delete[] szBuffer;
}
printf("OK\n");
}
cleanup:
// if we came across an error, delete the file we were creating
if(GetError() != 0)
{
remove(x_szXbeFilename);
printf("FAILED!\n");
printf("Xbe::Export: ERROR -> %s\n", GetError());
}
if(XbeFile != NULL)
{
fclose(XbeFile);
XbeFile = NULL;
}
return;
}
// constructor initialization
void Xbe::ConstructorInit()
{
m_HeaderEx = 0;
m_SectionHeader = 0;
m_szSectionName = 0;
m_LibraryVersion = 0;
m_KernelLibraryVersion = 0;
m_XAPILibraryVersion = 0;
m_TLS = 0;
m_bzSection = 0;
}
// returns xbe timestamp date as string (reuses ctime buffer)
static char *XbeTime(uint32 timestamp)
{
int v = 0;
time_t time = timestamp;
char *x_ctime = ctime(&time);
for(v = 0; x_ctime[v] != '\n'; v++)
;
x_ctime[v] = '\0';
return x_ctime;
}
// dump Xbe information to text file
void Xbe::DumpInformation(FILE *x_file)
{
if(GetError() != 0)
return;
time_t dwTimeDate = m_Header.dwTimeDate;
fprintf(x_file, "XBE information generated by CXBE (Version: " VERSION ")\n");
fprintf(x_file, "\n");
fprintf(x_file, "Title identified as \"%s\"\n", m_szAsciiTitle);
fprintf(x_file, "\n");
fprintf(x_file, "Dumping XBE file header...\n");
fprintf(x_file, "\n");
fprintf(x_file, "Magic Number : XBEH\n");
// print digital signature
{
fprintf(x_file, "Digitial Signature : <Hex Dump>");
for(int y = 0; y < 16; y++)
{
fprintf(x_file, "\n ");
for(int x = 0; x < 16; x++)
fprintf(x_file, "%.02X", m_Header.pbDigitalSignature[y * 16 + x]);
}
fprintf(x_file, "\n </Hex Dump>\n");
}
fprintf(x_file, "Base Address : 0x%.08X\n", m_Header.dwBaseAddr);
fprintf(x_file, "Size of Headers : 0x%.08X\n", m_Header.dwSizeofHeaders);
fprintf(x_file, "Size of Image : 0x%.08X\n", m_Header.dwSizeofImage);
fprintf(x_file, "Size of Image Header : 0x%.08X\n", m_Header.dwSizeofImageHeader);
fprintf(x_file, "TimeDate Stamp : 0x%.08X (%s)\n", m_Header.dwTimeDate,
XbeTime(dwTimeDate));
fprintf(x_file, "Certificate Address : 0x%.08X\n", m_Header.dwCertificateAddr);
fprintf(x_file, "Number of Sections : 0x%.08X\n", m_Header.dwSections);
fprintf(x_file, "Section Headers Address : 0x%.08X\n", m_Header.dwSectionHeadersAddr);
// print init flags
{
fprintf(x_file, "Init Flags : 0x%.08X ",
*(uint32 *)&m_Header.dwInitFlags);
if(m_Header.dwInitFlags.bMountUtilityDrive)
fprintf(x_file, "[Mount Utility Drive] ");
if(m_Header.dwInitFlags.bFormatUtilityDrive)
fprintf(x_file, "[Format Utility Drive] ");
if(m_Header.dwInitFlags.bLimit64MB)
fprintf(x_file, "[Limit Devkit Run Time Memory to 64MB] ");
if(!m_Header.dwInitFlags.bDontSetupHarddisk)
fprintf(x_file, "[Setup Harddisk] ");
fprintf(x_file, "\n");
}
char AsciiFilename[40] = { 0 };
setlocale(LC_ALL, "English");
char *wszFilename = (char *)GetAddr(m_Header.dwDebugUnicodeFilenameAddr);
if(wszFilename != NULL)
{
// wcstombs(AsciiFilename, wszFilename, 40);
char *c = wszFilename;
char *d = AsciiFilename;
while(*c)
{
*d++ = *c++;
c++;
}
}
else
{
AsciiFilename[0] = '\0';
}
fprintf(x_file,
"Entry Point : 0x%.08X (Retail: 0x%.08X, Debug: 0x%.08X)\n",
m_Header.dwEntryAddr, m_Header.dwEntryAddr ^ XOR_EP_RETAIL,
m_Header.dwEntryAddr ^ XOR_EP_DEBUG);
fprintf(x_file, "TLS Address : 0x%.08X\n", m_Header.dwTLSAddr);
fprintf(x_file, "(PE) Stack Commit : 0x%.08X\n", m_Header.dwPeStackCommit);
fprintf(x_file, "(PE) Heap Reserve : 0x%.08X\n", m_Header.dwPeHeapReserve);
fprintf(x_file, "(PE) Heap Commit : 0x%.08X\n", m_Header.dwPeHeapCommit);
fprintf(x_file, "(PE) Base Address : 0x%.08X\n", m_Header.dwPeBaseAddr);
fprintf(x_file, "(PE) Size of Image : 0x%.08X\n", m_Header.dwPeSizeofImage);
fprintf(x_file, "(PE) Checksum : 0x%.08X\n", m_Header.dwPeChecksum);
fprintf(x_file, "(PE) TimeDate Stamp : 0x%.08X (%s)\n", m_Header.dwPeTimeDate,
XbeTime(m_Header.dwPeTimeDate));
fprintf(x_file, "Debug Pathname Address : 0x%.08X (\"%s\")\n",
m_Header.dwDebugPathnameAddr, GetAddr(m_Header.dwDebugPathnameAddr));
fprintf(x_file, "Debug Filename Address : 0x%.08X (\"%s\")\n",
m_Header.dwDebugFilenameAddr, GetAddr(m_Header.dwDebugFilenameAddr));
fprintf(x_file, "Debug Unicode filename Address : 0x%.08X (L\"%s\")\n",
m_Header.dwDebugUnicodeFilenameAddr, AsciiFilename);
fprintf(x_file,
"Kernel Image Thunk Address : 0x%.08X (Retail: 0x%.08X, Debug: 0x%.08X)\n",
m_Header.dwKernelImageThunkAddr, m_Header.dwKernelImageThunkAddr ^ XOR_KT_RETAIL,
m_Header.dwKernelImageThunkAddr ^ XOR_KT_DEBUG);
fprintf(
x_file, "NonKernel Import Dir Address : 0x%.08X\n", m_Header.dwNonKernelImportDirAddr);
fprintf(x_file, "Library Versions : 0x%.08X\n", m_Header.dwLibraryVersions);
fprintf(x_file, "Library Versions Address : 0x%.08X\n", m_Header.dwLibraryVersionsAddr);
fprintf(x_file, "Kernel Library Version Address : 0x%.08X\n",
m_Header.dwKernelLibraryVersionAddr);
fprintf(
x_file, "XAPI Library Version Address : 0x%.08X\n", m_Header.dwXAPILibraryVersionAddr);
fprintf(x_file, "Logo Bitmap Address : 0x%.08X\n", m_Header.dwLogoBitmapAddr);
fprintf(x_file, "Logo Bitmap Size : 0x%.08X\n", m_Header.dwSizeofLogoBitmap);
fprintf(x_file, "\n");
fprintf(x_file, "Dumping XBE Certificate...\n");
fprintf(x_file, "\n");
fprintf(x_file, "Size of Certificate : 0x%.08X\n", m_Certificate.dwSize);
fprintf(x_file, "TimeDate Stamp : 0x%.08X (%s)\n", m_Certificate.dwTimeDate,
XbeTime(m_Certificate.dwTimeDate));
fprintf(x_file, "Title ID : 0x%.08X\n", m_Certificate.dwTitleId);
fprintf(x_file, "Title : L\"%s\"\n", m_szAsciiTitle);
// print alternate title IDs
{
fprintf(x_file, "Alternate Titles IDs : ");
for(int v = 0; v < 0x10; v++)
{
if(v != 0)
fprintf(x_file, " ");
fprintf(x_file, "0x%.08X", m_Certificate.dwAlternateTitleId[v]);
if(v != 0x0F)
fprintf(x_file, "\n");
}
fprintf(x_file, "\n");