-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
1287 lines (1184 loc) · 60.5 KB
/
Program.cs
File metadata and controls
1287 lines (1184 loc) · 60.5 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace XboxKit
{
internal class Program
{
// XISO Types: XGD1, XGD2, XGD2-Hybrid, XGD3
static readonly long[] XISO_OFFSET = [0x18300000, 0xFD90000, 0x89D80000, 0x2080000];
static readonly long[] XISO_LENGTH = [0x1A2DB0000, 0x1B3880000, 0xBF8A0000, 0x204510000];
// Redump ISO Types: XGD1, XGD2w0, XGD2w1, XGD2w2, XGD2w3+, XGD2-Hybrid, XGD3v0, XGD3
static readonly long[] REDUMP_ISO_LENGTH = [0x1D26A8000, 0x1D3301800, 0x1D2FEF800, 0x1D3082000, 0x1D3390000, 0x1D31A0000, 0x208E05800, 0x208E03800];
// Video Partition Types: XGD1, XGD2w0, XGD2w1, XGD2w2, XGD2w3, XGD2w4-7, XGD2w8-9, XGD2w10-12, XGD2w13, XGD2w14-15, XGD2w16, XGD2w17-18, XGD2w19, XGD2w20, XGD2-Hybrid, XGD3-beta XGD3v0, XGD3
static readonly long[] VIDEO_L0_LENGTH = [0xD58000, 0xA8000, 0x548000, 0x438000, 0x4BB0000, 0x56C0000, 0x5460000, 0x5BA0000, 0x5C10000, 0x55D0000, 0x55C0000, 0x8A40000, 0x8A90000, 0x8E80000, 0x4B1D0000, 0x1878000, 0x1880000, 0x1880000];
static readonly long[] VIDEO_L1_LENGTH = [0x50000, 0x9800, 0x197800, 0x11A000, 0x4BA0000, 0x56B0000, 0x5450000, 0x5B90000, 0x5C00000, 0x55C0000, 0x55B0000, 0x8A30000, 0x8A80000, 0x8E70000, 0x4AFD0000, 0x186D800, 0x1875800, 0x1873800];
static readonly long[] VIDEO_LENGTH = new long[VIDEO_L0_LENGTH.Length];
// Wave Types: XGD2w0, XGD2w1, XGD2w2, XGD2w3, XGD2w4, XGD2w5, XGD2w6, XGD2w7, XGD2w8, XGD2w9, XGD2w10, XGD2w11, XGD2w12, XGD2w13, XGD2w14, XGD2w15, XGD2w16, XGD2w17, XGD2w18, XGD2w19, XGD2w20, XGD2-Hybrid, XGD1 XGD3-beta
static readonly string[] WAVE_PVD = ["2004083110334900", "2005100712184600", "2006030621090700", "2009011416000000", "2009082417000000", "2009100517000000", "2009102917000000", "2010022116000000", "2010090417000000", "2010091517000000", "2010102817000000", "2011011816000000", "2011061217000000", "2011071217000000", "2011120716000000", "2012022116000000", "2012062117000000", "2012110716000000", "2012111816000000", "2013082617000000", "2015042617000000", "2006041012132800", "2001091310425500", "2010121616000000"];
// Print help for invalid command
static void PrintHelp()
{
Console.WriteLine("XboxKit (c) Deterous 2024-2025");
Console.WriteLine("");
Console.WriteLine("Usage: xboxkit.exe [options] <input.iso> [video.iso] [filler_data] [system_update_file]");
Console.WriteLine("");
Console.WriteLine("Rebuild mode: Combine input files (no options)");
Console.WriteLine("Extract mode: Use options (other paths are used for custom output file names)");
Console.WriteLine("-a, --all \t Perform all operations (-rstuvwx) on the input ISO");
Console.WriteLine("-q, --quiet \t Don't print INFO messages to console");
Console.WriteLine("-r, --random\t Extracts random filler data to a separate file");
Console.WriteLine("-s, --seed \t Extracts RNG seed used for XGD1 filler");
Console.WriteLine("-t, --trim \t Trims end of XISO (game partition)");
Console.WriteLine("-u, --update\t Extracts update file from video ISO (XGD3 only)");
Console.WriteLine("-v, --video \t Extracts video ISO (video partition)");
Console.WriteLine("-w, --wipe \t Wipes filler data in XISO");
Console.WriteLine("-x, --xiso \t Extracts XISO (game partition)");
}
static void Main(string[] args)
{
#region Initial Setup
// Initialize VIDEO_LENGTH array
for (int i = 0; i < VIDEO_LENGTH.Length; i++)
VIDEO_LENGTH[i] = VIDEO_L0_LENGTH[i] + VIDEO_L1_LENGTH[i];
bool help = false;
bool quiet = false;
bool extractXISO = false;
bool extractVideo = false;
bool extractFiller = false;
bool extractSeed = false;
bool trimXISO = false;
bool wipeXISO = false;
bool extractUpdate = false;
string isoPath = string.Empty;
string videoPath = string.Empty;
string fillerPath = string.Empty;
string seedPath = string.Empty;
string sectorsTXTPath = string.Empty;
string updatePath = string.Empty;
List<string> filePaths = new();
// Check arguments
if (args.Length == 0)
{
PrintHelp();
return;
}
foreach (var arg in args)
{
if (arg.StartsWith("--"))
{
switch (arg.ToLowerInvariant())
{
case "--help":
help = true;
break;
case "--quiet":
quiet = true;
break;
case "--all":
extractFiller = true;
extractSeed = true;
trimXISO = true;
extractUpdate = true;
extractVideo = true;
wipeXISO = true;
extractXISO = true;
break;
case "--random":
extractFiller = true;
break;
case "--seed":
extractSeed = true;
break;
case "--trim":
trimXISO = true;
break;
case "--update":
extractUpdate = true;
break;
case "--video":
extractVideo = true;
break;
case "--wipe":
wipeXISO = true;
break;
case "--xiso":
extractXISO = true;
break;
default:
filePaths.Add(arg);
break;
}
}
else if (arg.StartsWith("-") && !arg.StartsWith("--"))
{
foreach (char flag in arg.Substring(1).ToLowerInvariant())
{
switch (flag)
{
case 'h':
help = true;
break;
case 'q':
quiet = true;
break;
case 'a':
extractFiller = true;
extractSeed = true;
trimXISO = true;
extractUpdate = true;
extractVideo = true;
wipeXISO = true;
extractXISO = true;
break;
case 'r':
extractFiller = true;
break;
case 's':
extractSeed = true;
break;
case 't':
trimXISO = true;
break;
case 'u':
extractUpdate = true;
break;
case 'v':
extractVideo = true;
break;
case 'w':
wipeXISO = true;
break;
case 'x':
extractXISO = true;
break;
default:
Console.WriteLine($"[ERROR] Unknown flag: -{flag}");
PrintHelp();
return;
}
}
}
else
{
filePaths.Add(arg);
}
}
if (help)
{
PrintHelp();
return;
}
if (filePaths.Count > 0)
isoPath = filePaths[0];
if (filePaths.Count > 1)
videoPath = filePaths[1];
if (filePaths.Count > 2)
fillerPath = filePaths[2];
if (filePaths.Count > 3)
updatePath = filePaths[3];
// Determine input filenames
string dir = Path.GetDirectoryName(isoPath);
string filename = Path.GetFileNameWithoutExtension(isoPath);
string extension = Path.GetExtension(isoPath);
if (string.IsNullOrEmpty(isoPath) || !File.Exists(isoPath))
{
Console.WriteLine($"[ERROR] Invalid file path: {isoPath}");
return;
}
// Determine output filenames
if (string.IsNullOrEmpty(videoPath))
videoPath = Path.Combine(dir, $"{filename}.video.iso");
if (string.IsNullOrEmpty(fillerPath))
fillerPath = Path.Combine(dir, $"{filename}.filler");
if (string.IsNullOrEmpty(seedPath))
seedPath = Path.Combine(dir, $"{filename}.seed");
if (string.IsNullOrEmpty(sectorsTXTPath))
sectorsTXTPath = Path.Combine(dir, "sectors.txt");
if (string.IsNullOrEmpty(updatePath))
updatePath = Path.Combine(dir, "su20076000_00000000");
string xisoPath = Path.Combine(dir, $"{filename}.xiso");
string redumpPath = Path.Combine(dir, $"{filename}.redump.iso");
// Compare input ISO file size to determine file type
FileInfo isoInfo = new(isoPath);
long isoSize = isoInfo.Length;
int redumpIsoType = Array.IndexOf(REDUMP_ISO_LENGTH, isoSize);
int videoIsoType = Array.IndexOf(VIDEO_LENGTH, isoSize);
int xisoType = Array.IndexOf(XISO_LENGTH, isoSize);
#endregion
if (redumpIsoType >= 0)
{
#region Mode 1: Redump ISO as input
if (!extractFiller && !extractSeed && !extractUpdate && !extractVideo && !extractXISO)
{
Console.WriteLine("[INFO] Redump ISO provided with no options, nothing to do");
PrintHelp();
return;
}
if (wipeXISO && !extractXISO && !quiet)
Console.WriteLine("[INFO] Wiping XISO option (-w) does nothing without extracting XISO (-x)");
if (trimXISO && !extractXISO && !quiet)
Console.WriteLine("[INFO] Trimming XISO option (-t) does nothing without extracting XISO (-x)");
if (extractXISO && extractFiller && !wipeXISO)
{
Console.WriteLine("[ERROR] Cannot write filler data without wiping XISO");
Console.WriteLine(" For now, use -w with -s");
return;
}
// Check that XISO doesn't already exist
if (extractXISO && File.Exists(xisoPath))
{
Console.WriteLine($"[ERROR] File already exists: {xisoPath}");
return;
}
// Check that video ISO doesn't already exist
if (extractVideo && File.Exists(videoPath))
{
Console.WriteLine($"[ERROR] File already exists: {videoPath}");
return;
}
// Check that filler data file doesn't already exist
if (extractFiller && File.Exists(fillerPath))
{
Console.WriteLine($"[ERROR] File already exists: {fillerPath}");
return;
}
// Check that update file doesn't already exist
if (extractUpdate && File.Exists(updatePath))
{
Console.WriteLine($"[ERROR] File already exists: {updatePath}");
return;
}
// Check that seed file doesn't already exist
if (extractSeed && File.Exists(seedPath))
{
Console.WriteLine($"[ERROR] File already exists: {seedPath}");
return;
}
// Determine disc layout type
long xgdType = redumpIsoType switch
{
0 => 0, // XGD1
1 or 2 or 3 or 4 => 1, // XGD2
5 => 2, // XGD2 (Hybrid)
6 or 7 => 3, // XGD3
_ => 0,
};
// Open redump ISO for reading
using FileStream isoFS = new(isoPath, FileMode.Open, FileAccess.Read, FileShare.Read);
if (!quiet)
Console.WriteLine($"[INFO] Reading redump ISO from {isoPath}");
// Extract video partition
if (extractVideo)
{
// Compare PVD creation datetime against known datetimes to determine wave
int? wave = null;
if (redumpIsoType == 4 || redumpIsoType == 6)
{
try
{
isoFS.Seek(0x832D, SeekOrigin.Begin);
byte[] pvd = new byte[16];
int bytesRead = isoFS.Read(pvd, 0, pvd.Length);
if (bytesRead == 16)
{
wave = Array.IndexOf(WAVE_PVD, Encoding.ASCII.GetString(pvd));
}
else
{
Console.WriteLine($"[ERROR] Failed to read PVD from {isoPath}");
return;
}
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR] {ex.Message}");
return;
}
}
// Determine size of output video ISO
int videoType = redumpIsoType switch
{
0 => 0, // XGD1
1 => 1, // XGD2 Wave 0
2 => 2, // XGD2 Wave 1
3 => 3, // XGD2 Wave 2
4 => wave switch // XGD2 Wave 3-20
{
0 => 1, // E9B8ECFE
1 => 2, // 739CEAB3
2 => 3, // A4CFB59C
3 => 4, // 2A4CCBD3
4 or 5 or 6 or 7 => 5, // 05C6C409
8 or 9 => 6, // 0441D6A5
10 or 11 or 12 => 7, // E18BC70B
13 => 8, // 40DCB18F
14 or 15 => 9, // 23A198FC
16 => 10, // AB25DB47
17 or 18 => 11, // 169EF597
19 => 12, // 169EF597
20 => 13, // 032CCF37
21 => 14, // F48D24B8
22 => 0, // 8FC52135
_ => -1,
},
5 => 14, // XGD2 (Hybrid)
6 => wave switch // XGD3-beta or XGD3v0
{
23 => 15, // XGD3-beta
_ => 16, // XGD3v0
},
7 => 17, // XGD3
_ => -1,
};
if (videoType == -1)
{
Console.WriteLine("[ERROR] Unexpected video partition. Cannot determine wave");
return;
}
// Create file for video partition
using FileStream videoFS = new(videoPath, FileMode.Create, FileAccess.Write, FileShare.None);
if (!quiet)
Console.WriteLine($"[INFO] Writing video partition to {videoPath}");
// Write layer 0 portion of video partition
long l0Length = VIDEO_L0_LENGTH[videoType];
if (!Utils.WriteBytes(isoFS, videoFS, 0, l0Length))
{
Console.WriteLine($"[ERROR] Failed writing video partition.");
return;
}
// Write layer 1 portion of video partition
long l1Length = VIDEO_L1_LENGTH[videoType];
if (!Utils.WriteBytes(isoFS, videoFS, isoSize - l1Length, l1Length))
{
Console.WriteLine("[ERROR] Failed reading video partition.");
return;
}
}
// Extract system update file from XGD3 video partition
if (extractUpdate && xgdType == 3)
{
// Open video ISO for reading and writing
using FileStream videoFS = new(videoPath, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
long videoLength = videoFS.Length;
long updateOffset = XDVDFS.SUOffset(videoFS);
// Write update file contents to file
if (!quiet)
Console.WriteLine($"[INFO] Writing system update file to {updatePath}");
using FileStream updateFS = new(updatePath, FileMode.Create, FileAccess.Write, FileShare.None);
long updateLength = videoLength - updateOffset - Utils.SECTOR_SIZE;
if (!Utils.WriteBytes(videoFS, updateFS, updateOffset, updateLength))
{
Console.WriteLine($"[ERROR] Failed writing system update file.");
return;
}
// Zero update file within XISO
if (!quiet)
Console.WriteLine($"[INFO] Zeroing system update file in {videoPath}");
Utils.WriteZeroes(videoFS, updateOffset, updateLength);
}
// If XGD1, try brute force the filler data seed
uint xgd1Seed;
if (extractSeed)
{
if (xgdType == 0)
{
// Validate XGD1 magic bytes
byte[] magic = new byte[XDVDFS.MAGIC.Length];
if (!Utils.WriteBytes(isoFS, magic, XISO_OFFSET[xgdType] + 0x10800))
{
Console.WriteLine("[ERROR] Failed reading XGD1 XDVDFS.");
return;
}
if (!magic.SequenceEqual(XDVDFS.MAGIC))
{
Console.WriteLine("[ERROR] Invalid data in XDVDFS volume descriptor.");
return;
}
// Determine version offset
byte[] nextBuf = new byte[8];
if (!Utils.WriteBytes(isoFS, nextBuf, XISO_OFFSET[xgdType] + 0x10820))
{
Console.WriteLine("[ERROR] Failed reading XGD1 XDVDFS volume descriptor.");
return;
}
int versionOffset = 0x10824;
if (nextBuf.SequenceEqual(new byte[8]))
versionOffset += 0x10;
// Determine XGD1 version
byte[] versionBuf = new byte[2];
if (!Utils.WriteBytes(isoFS, versionBuf, XISO_OFFSET[xgdType] + versionOffset))
{
Console.WriteLine("[ERROR] Failed to read XGD1 version.");
return;
}
ushort version = (ushort)(versionBuf[0] | (versionBuf[1] << 8));
if (version == 0)
{
Console.WriteLine("[ERROR] Invalid XGD1 version (0)");
return;
}
else if (!quiet)
Console.WriteLine($"[INFO] XGD1 Version: {version}");
// Determine XGD1 pseudo random number generator seed, if possible
byte[] firstXISOSector = new byte[Utils.SECTOR_SIZE * 2];
if (!Utils.WriteBytes(isoFS, firstXISOSector, XISO_OFFSET[xgdType]))
{
Console.WriteLine("[ERROR] Failed reading first XISO sector");
return;
}
if (XboxPRNG.GuessSeed(firstXISOSector, out uint seed))
{
xgd1Seed = seed;
if (!quiet)
Console.WriteLine($"[INFO] Filler data seed: {seed:X8}");
using FileStream seedFS = new(seedPath, FileMode.Create, FileAccess.Write, FileShare.None);
byte[] seedBytes = BitConverter.GetBytes(seed);
seedFS.Write(seedBytes, 0, seedBytes.Length);
if (!quiet)
Console.WriteLine($"[INFO] Writing filler data to {seedPath}");
}
}
}
// Quit early if we're not extracting data from XISO
if (!extractXISO && !extractFiller)
return;
// Parse XISO filesystem for all file extents
List<(uint Start, uint End)> validRanges = XDVDFS.GetXISORanges(isoFS, XISO_OFFSET[xgdType], quiet);
if (!quiet)
{
foreach (var (start, end) in validRanges)
Console.WriteLine($"[INFO] XISO File Extent: {start}-{end}");
}
// Create file for game partition
FileStream xisoFS = null!;
if (extractXISO)
{
xisoFS = new FileStream(xisoPath, FileMode.Create, FileAccess.Write, FileShare.None);
if (!quiet)
Console.WriteLine($"[INFO] Writing game partition to {xisoPath}");
}
// Create file for filler data
FileStream fillerFS = null!;
if (extractFiller)
{
fillerFS = new FileStream(fillerPath, FileMode.Create, FileAccess.Write, FileShare.None);
if (!quiet)
Console.WriteLine($"[INFO] Writing random filler data to {fillerPath}");
}
// Process XISO
isoFS.Seek(XISO_OFFSET[xgdType], SeekOrigin.Begin);
long xisoLength = XISO_LENGTH[xgdType];
long numBytes = 0;
while (numBytes < xisoLength)
{
long currentByte = XISO_OFFSET[xgdType] + numBytes;
long currentSector = (currentByte + Utils.SECTOR_SIZE - 1) / Utils.SECTOR_SIZE;
long bytesUntilEndOfExtent = 0;
long bytesToWipe = 0;
bool skipEnd = false;
// Determine whether current sector is after last file extent
if (validRanges.Count > 0 && currentSector > validRanges[validRanges.Count - 1].End)
{
// Remainder of XISO is filler
long bytesUntilEnd = xisoLength - numBytes;
if (extractFiller || wipeXISO)
bytesToWipe = bytesUntilEnd;
// Trim XISO
if (trimXISO)
{
skipEnd = true;
if (!quiet)
Console.WriteLine($"[INFO] Trimming XISO");
}
if (trimXISO && !extractFiller)
{
// Nothing else to do, finish processing XISO early
numBytes += bytesUntilEnd;
break;
}
}
else if (extractFiller || wipeXISO || trimXISO)
{
// Determine whether current sector is within a file extent or filler data
for (int i = 0; i < validRanges.Count; i++)
{
if (currentSector >= validRanges[i].Start && currentSector <= validRanges[i].End)
{
// Number of bytes remaining in current file extent
bytesUntilEndOfExtent = (validRanges[i].End + 1) * Utils.SECTOR_SIZE - currentByte;
break;
}
else if (currentSector < validRanges[i].Start && (i == 0 || currentSector > validRanges[i - 1].End))
{
// Wipe until next file extent
bytesToWipe = validRanges[i].Start * Utils.SECTOR_SIZE - currentByte;
break;
}
}
}
// Write filler data to file
if (extractFiller)
{
if (bytesToWipe > 0)
{
if (!Utils.WriteBytes(isoFS, fillerFS, -1, bytesToWipe))
{
Console.WriteLine($"[ERROR] Failed writing filler data.");
return;
}
if (!extractXISO)
numBytes += bytesToWipe;
}
else if (!extractXISO)
{
// Skip file extent
long bytesToEnd;
if (bytesUntilEndOfExtent > 0)
bytesToEnd = bytesUntilEndOfExtent;
else
bytesToEnd = xisoLength - numBytes;
isoFS.Seek(bytesToEnd, SeekOrigin.Current);
numBytes += bytesToEnd;
}
}
// Write to XISO file
if (extractXISO)
{
if (wipeXISO && bytesToWipe > 0 && !skipEnd)
{
// Validity check
if (bytesToWipe % Utils.SECTOR_SIZE != 0)
{
Console.WriteLine("[ERROR] Unexpected Error 2, please report this.");
return;
}
// Write zeroes to XISO (unless trimming end)
Utils.WriteZeroes(xisoFS, -1, bytesToWipe);
numBytes += bytesToWipe;
// Move ahead in ISO file if filler was not read
if (!extractFiller)
isoFS.Seek(bytesToWipe, SeekOrigin.Current);
}
else if (!skipEnd)
{
// Write data to XISO
long bytesToRead;
if (bytesToWipe > 0)
bytesToRead = bytesToWipe;
else if (bytesUntilEndOfExtent > 0)
bytesToRead = bytesUntilEndOfExtent;
else
bytesToRead = xisoLength - numBytes;
if (!Utils.WriteBytes(isoFS, xisoFS, -1, bytesToRead))
{
Console.WriteLine($"[ERROR] Failed writing game partition (XISO).");
return;
}
numBytes += bytesToRead;
}
else if (bytesToWipe > 0)
{
isoFS.Seek(bytesToWipe, SeekOrigin.Current);
numBytes += bytesToWipe;
}
}
}
// Close files
if (xisoFS != null)
xisoFS.Dispose();
if (fillerFS != null)
fillerFS.Dispose();
// Validity check
if (numBytes != xisoLength)
{
Console.WriteLine("[ERROR] Unexpected Error 3, please report this");
return;
}
#endregion
}
else if (videoIsoType >= 0)
{
#region Mode 2: Video ISO as input
// Check for valid options
if (extractVideo)
{
Console.WriteLine("[ERROR] Cannot extract video (-v), input file is already video.");
return;
}
if (extractXISO)
{
Console.WriteLine("[ERROR] Cannot extract XISO (-x), input file is video.");
return;
}
if (extractFiller)
{
Console.WriteLine("[ERROR] Cannot extract filler (-s), input file is video.");
return;
}
if (wipeXISO)
{
Console.WriteLine("[ERROR] Cannot wipe XISO (-w), input file is video.");
return;
}
if (trimXISO)
{
Console.WriteLine("[ERROR] Cannot trim XISO (-t), input file is video.");
return;
}
if (!extractUpdate)
{
Console.WriteLine("[ERROR] Use -u flag to extract system update from video partition.");
return;
}
// Check that update file doesn't already exist
if (File.Exists(updatePath))
{
Console.WriteLine($"[ERROR] System update file already exists: {updatePath}");
return;
}
// Check that video partition is from XGD3 disc
if (videoIsoType != 15 && videoIsoType != 16 && videoIsoType != 17)
{
Console.WriteLine("[ERROR] Can only extract su20076000_00000000 from XGD3 video partitions.");
return;
}
// Open ISO for reading and writing
using FileStream videoFS = new(isoPath, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
long updateOffset = XDVDFS.SUOffset(videoFS);
if (!quiet)
Console.WriteLine($"[INFO] Writing system update file to {updatePath}");
using FileStream updateFS = new(updatePath, FileMode.Create, FileAccess.Write, FileShare.None);
long updateLength = videoFS.Length - updateOffset - Utils.SECTOR_SIZE;
if (!Utils.WriteBytes(videoFS, updateFS, updateOffset, updateLength))
{
Console.WriteLine($"[ERROR] Failed writing system update file.");
return;
}
// Zero out the update file in the video ISO
Utils.WriteZeroes(videoFS, updateOffset, updateLength);
#endregion
}
else
{
// Mode 3: XISO as input
long xisoLength = isoSize; // Later updated to intended length if trimmed
#region Wipe XISO
// Check for invalid options
bool invalidOptions = false;
if (extractXISO)
{
Console.WriteLine("[ERROR] Cannot extract XISO (-x), input file is already XISO");
invalidOptions = true;
}
if (extractVideo)
{
Console.WriteLine("[ERROR] Cannot extract video (-v), input file is XISO");
invalidOptions = true;
}
if (extractUpdate)
{
Console.WriteLine("[ERROR] Cannot extract update (-u), input file is XISO");
invalidOptions = true;
}
if (invalidOptions)
return;
using FileStream isoFS = new(isoPath, FileMode.Open, FileAccess.Read, FileShare.Read);
if (!quiet)
Console.WriteLine($"[INFO] Reading XISO from {isoPath}");
bool writeXISO = wipeXISO || trimXISO;
if (extractFiller || writeXISO)
{
// Cannot extract/wipe/trim from invalid XISO size
if (xisoType < 0)
{
Console.WriteLine("[ERROR] Unexpected XISO size. Your file may be trimmed or corrupt.");
Console.WriteLine(" Use the full XISO if you want to trim/wipe/extract filler.");
return;
}
// Create file for game partition
FileStream xisoFS = null!;
if (writeXISO)
xisoFS = new FileStream(xisoPath, FileMode.Create, FileAccess.Write, FileShare.None);
// Create file for filler data
FileStream fillerFS = null!;
if (extractFiller)
fillerFS = new FileStream(fillerPath, FileMode.Create, FileAccess.Write, FileShare.None);
// Parse XISO filesystem for all file extents
List<(uint Start, uint End)> validRanges = XDVDFS.GetXISORanges(isoFS, 0, quiet);
if (!quiet)
{
foreach (var (start, end) in validRanges)
Console.WriteLine($"[INFO] XISO File Extent: {start}-{end}");
}
if (extractFiller && !quiet)
Console.WriteLine($"[INFO] Extracting filler data to {fillerPath}");
if (wipeXISO && !quiet)
Console.WriteLine($"[INFO] Writing wiped XISO to {xisoPath}");
if (!wipeXISO && trimXISO && !quiet)
Console.WriteLine($"[INFO] Writing XISO to {xisoPath}");
isoFS.Seek(0, SeekOrigin.Begin);
long currentByte = 0;
while (currentByte < isoSize)
{
long currentSector = (currentByte + Utils.SECTOR_SIZE - 1) / Utils.SECTOR_SIZE;
long bytesUntilEndOfExtent = 0;
long bytesToWipe = 0;
bool skipEnd = false;
// Determine whether current sector is after last file extent
if (validRanges.Count > 0 && currentSector > validRanges[validRanges.Count - 1].End)
{
// Remainder of XISO is filler
long bytesUntilEnd = isoSize - currentByte;
if (extractFiller || wipeXISO)
bytesToWipe = bytesUntilEnd;
// Trim XISO
if (trimXISO)
{
skipEnd = true;
if (!quiet)
Console.WriteLine($"[INFO] Trimming XISO");
}
if (trimXISO && !extractFiller)
{
// Nothing else to do, finish processing XISO early
currentByte += bytesUntilEnd;
break;
}
}
else if (extractFiller || writeXISO)
{
// Determine whether current sector is within a file extent or filler data
for (int i = 0; i < validRanges.Count; i++)
{
if (currentSector >= validRanges[i].Start && currentSector <= validRanges[i].End)
{
// Number of bytes remaining in current file extent
bytesUntilEndOfExtent = (validRanges[i].End + 1) * Utils.SECTOR_SIZE - currentByte;
break;
}
else if (currentSector < validRanges[i].Start && (i == 0 || currentSector > validRanges[i - 1].End))
{
// Wipe until next file extent
bytesToWipe = validRanges[i].Start * Utils.SECTOR_SIZE - currentByte;
break;
}
}
}
// Write filler data to file
if (extractFiller)
{
if (bytesToWipe > 0)
{
if (!Utils.WriteBytes(isoFS, fillerFS, -1, bytesToWipe))
{
Console.WriteLine($"[ERROR] Failed writing filler data.");
return;
}
if (!writeXISO)
currentByte += bytesToWipe;
}
else if (!writeXISO)
{
// Skip file extent
long bytesToEnd;
if (bytesUntilEndOfExtent > 0)
bytesToEnd = bytesUntilEndOfExtent;
else
bytesToEnd = isoSize - currentByte;
isoFS.Seek(bytesToEnd, SeekOrigin.Current);
currentByte += bytesToEnd;
}
}
// Write to XISO file
if (writeXISO)
{
if (wipeXISO && bytesToWipe > 0 && !skipEnd)
{
// Validity check
if (bytesToWipe % Utils.SECTOR_SIZE != 0)
{
Console.WriteLine("[ERROR] Unexpected Error 4, please report this.");
return;
}
// Write zeroes to XISO (unless trimming end)
Utils.WriteZeroes(xisoFS, -1, bytesToWipe);
currentByte += bytesToWipe;
// Move ahead in ISO file if filler was not read
if (!extractFiller)
isoFS.Seek(bytesToWipe, SeekOrigin.Current);
}
else if (!skipEnd)
{
// Write data to XISO
long bytesToRead;
if (bytesToWipe > 0)
bytesToRead = bytesToWipe;
else if (bytesUntilEndOfExtent > 0)
bytesToRead = bytesUntilEndOfExtent;
else
bytesToRead = isoSize - currentByte;
if (!Utils.WriteBytes(isoFS, xisoFS, -1, bytesToRead))
{
Console.WriteLine($"[ERROR] Failed writing game partition (XISO).");
return;
}
currentByte += bytesToRead;
}
else if (bytesToWipe > 0)
{
// Trim end of XISO
isoFS.Seek(bytesToWipe, SeekOrigin.Current);
currentByte += bytesToWipe;
}
}
}
// Close files
if (xisoFS != null)
xisoFS.Dispose();
if (fillerFS != null)
fillerFS.Dispose();
// Validity check
if (currentByte != isoSize)
{
Console.WriteLine("[ERROR] Unexpected Error 5, please report this");
return;
}
return;
}
#endregion
#region Rebuild Redump ISO
// Check that video partition exists
if (!File.Exists(videoPath))
{
Console.WriteLine($"[ERROR] Invalid file path: {videoPath}");
Console.WriteLine("Provide a file path to the video partition to rebuild the redump ISO.");
return;
}
if (xisoType < 0 && !File.Exists(fillerPath) && !File.Exists(seedPath))
{
Console.WriteLine("[ERROR] Unexpected XISO size. Your XISO may be trimmed or corrupt.");
Console.WriteLine(" Cannot rebuild redump ISO from trimmed XISO without filler or seed.");
return;
}
// Determine video type based on video partition size
FileInfo videoInfo = new(videoPath);
long videoSize = videoInfo.Length;
int videoType = Array.IndexOf(VIDEO_LENGTH, videoSize);
if (videoType < 0)
{
Console.WriteLine("[ERROR] Unexpected video partition ISO size. Your video file may be trimmed or corrupt.");
return;
}
// Determine length of output redump ISO
long redumpLength = videoType switch
{
0 => REDUMP_ISO_LENGTH[0], // XGD1
1 => REDUMP_ISO_LENGTH[1], // XGD2w0
2 => REDUMP_ISO_LENGTH[2], // XGD2w1
3 => REDUMP_ISO_LENGTH[3], // XGD2w2
4 or 5 or 6 or 7 or 8 or 9 or 10 or 11 or 12 or 13 => REDUMP_ISO_LENGTH[4], // XGD2w3+
14 => REDUMP_ISO_LENGTH[5], // XGD2 (Hybrid)
15 or 16 => REDUMP_ISO_LENGTH[6], // XGD3-beta, XGD3v0
17 => REDUMP_ISO_LENGTH[7], // XGD3
_ => 0,
};
// Determine intended xisoType based on video ISO length
xisoType = videoType switch
{
0 => 0, // XGD1
1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 or 10 or 11 or 12 or 13 => 1, // XGD2
14 => 2, // XGD2 (Hybrid)
15 or 16 or 17 => 3, // XGD3
_ => 0,
};
xisoLength = XISO_LENGTH[xisoType];
// Create redump ISO
using FileStream redumpFS = new(redumpPath, FileMode.Create, FileAccess.Write, FileShare.None);
if (!quiet)
Console.WriteLine($"[INFO] Writing redump ISO to {redumpPath}");
// Open video ISO for reading
using FileStream videoFS = new(videoPath, FileMode.Open, FileAccess.Read, FileShare.Read);
if (!quiet)
Console.WriteLine($"[INFO] Reading video partition from {videoPath}");
// Write Layer 0 portion of video partition
long l0Length = VIDEO_L0_LENGTH[videoType];
if (!Utils.WriteBytes(videoFS, redumpFS, 0, l0Length))
{
Console.WriteLine($"[ERROR] Failed writing layer 0 portion of video partition.");
return;
}
// Write layer 0 padding
long xisoOffset = XISO_OFFSET[xisoType];
long l0Padding = xisoOffset - l0Length;
Utils.WriteZeroes(redumpFS, -1, l0Padding);
// Write game partition