-
Notifications
You must be signed in to change notification settings - Fork 6
/
StfsStructs.cs
707 lines (623 loc) · 20.1 KB
/
StfsStructs.cs
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
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace XboxWinFsp
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct PEC_HEADER
{
public XE_CONSOLE_SIGNATURE ConsoleSignature;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x14)]
public byte[] ContentId;
public ulong Unknown;
public STF_VOLUME_DESCRIPTOR StfsVolumeDescriptor;
public uint Unknown2;
public ulong Creator;
public byte ConsoleIdsCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)]
public XE_CONSOLE_ID[] ConsoleIds; // AKA AuditList ?
public void EndianSwap()
{
ConsoleSignature.EndianSwap();
Unknown = Unknown.EndianSwap();
StfsVolumeDescriptor.EndianSwap();
Unknown2 = Unknown2.EndianSwap();
Creator = Creator.EndianSwap();
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct STF_VOLUME_DESCRIPTOR
{
public byte DescriptorLength;
public byte Version;
public byte Flags;
public ushort DirectoryAllocationBlocks;
public byte DirectoryFirstBlockNumber0;
public byte DirectoryFirstBlockNumber1;
public byte DirectoryFirstBlockNumber2;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x14)]
public byte[] RootHash;
public uint NumberOfTotalBlocks;
public uint NumberOfFreeBlocks;
public static STF_VOLUME_DESCRIPTOR Default()
{
var descriptor = new STF_VOLUME_DESCRIPTOR();
descriptor.DescriptorLength = 0x24;
descriptor.Version = 0;
descriptor.Flags = 0;
descriptor.DirectoryAllocationBlocks = 1;
descriptor.DirectoryFirstBlockNumber0 =
descriptor.DirectoryFirstBlockNumber1 =
descriptor.DirectoryFirstBlockNumber2 = 0;
descriptor.RootHash = new byte[0x14];
descriptor.NumberOfTotalBlocks = 1;
descriptor.NumberOfFreeBlocks = 0;
return descriptor;
}
public bool ReadOnlyFormat
{
get
{
return (Flags & 1) == 1;
}
}
public bool RootActiveIndex
{
get
{
return (Flags & 2) == 2;
}
}
public int DirectoryFirstBlockNumber
{
get
{
return DirectoryFirstBlockNumber0 | (DirectoryFirstBlockNumber1 << 8) | (DirectoryFirstBlockNumber2 << 16);
}
}
public void EndianSwap()
{
// DirectoryAllocationBlocks = DirectoryAllocationBlocks.EndianSwap();
NumberOfTotalBlocks = NumberOfTotalBlocks.EndianSwap();
NumberOfFreeBlocks = NumberOfFreeBlocks.EndianSwap();
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct STF_HASH_ENTRY
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x14)]
public byte[] Hash;
public byte Flags;
public byte Level0NextBlock2;
public byte Level0NextBlock1;
public byte Level0NextBlock0;
public bool LevelNActiveIndex
{
get
{
return (Flags & 0x40) == 0x40;
}
}
public int Level0NextBlock
{
get
{
return Level0NextBlock0 | (Level0NextBlock1 << 8) | (Level0NextBlock2 << 16);
}
set
{
Level0NextBlock0 = (byte)(value & 0xFF);
Level0NextBlock1 = (byte)((value >> 8) & 0xFF);
Level0NextBlock2 = (byte)((value >> 16) & 0xFF);
}
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct STF_HASH_BLOCK
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 170)]
public STF_HASH_ENTRY[] Entries;
public uint NumberOfCommittedBlocks;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
public byte[] Padding;
public void EndianSwap()
{
NumberOfCommittedBlocks = NumberOfCommittedBlocks.EndianSwap();
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct XCONTENT_LICENSEE
{
public const ulong kTypeWindowsId = 3u << 48;
public const ulong kTypeXuid = 9u << 48;
public const ulong kTypeSerPrivileges = 0xB000u << 48;
public const ulong kTypeHvFlags = 0xC000u << 48;
public const ulong kTypeKeyVaultPrivileges = 0xD000u << 48;
public const ulong kTypeMediaFlags = 0xE000u << 48;
public const ulong kTypeConsoleId = 0xF000u << 48;
public const ulong kTypeUnrestricted = 0xFFFFu << 48;
public ulong LicenseeId;
public uint LicenseBits;
public uint LicenseFlags;
public bool IsWindowsIdLicense
{
get
{
return (LicenseeId & kTypeWindowsId) == kTypeWindowsId;
}
}
public bool IsXuidLicense
{
get
{
return (LicenseeId & kTypeXuid) == kTypeXuid;
}
}
public bool IsSerPrivilegesLicense
{
get
{
return (LicenseeId & kTypeSerPrivileges) == kTypeSerPrivileges;
}
}
public bool IsHvFlagsLicense
{
get
{
return (LicenseeId & kTypeHvFlags) == kTypeHvFlags;
}
}
public bool IsKeyVaultPrivilegesLicense
{
get
{
return (LicenseeId & kTypeKeyVaultPrivileges) == kTypeKeyVaultPrivileges;
}
}
public bool IsMediaFlagsLicense
{
get
{
return (LicenseeId & kTypeMediaFlags) == kTypeMediaFlags;
}
}
public bool IsConsoleIdLicense
{
get
{
return (LicenseeId & kTypeConsoleId) == kTypeConsoleId;
}
}
public bool IsUnrestrictedLicense
{
get
{
return (LicenseeId & kTypeUnrestricted) == kTypeUnrestricted;
}
}
public string LicenseType
{
get
{
if (IsUnrestrictedLicense)
return "Unrestricted";
if (IsWindowsIdLicense)
return "WindowsId";
if (IsXuidLicense)
return "Xuid";
if (IsSerPrivilegesLicense)
return "SerPrivileges";
if (IsHvFlagsLicense)
return "HvFlags";
if (IsKeyVaultPrivilegesLicense)
return "KeyVaultPrivileges";
if (IsMediaFlagsLicense)
return "MediaFlags";
if (IsConsoleIdLicense)
return "ConsoleId";
return "Unknown";
}
}
public bool IsValid
{
get
{
return LicenseeId != 0 || LicenseBits != 0 || LicenseFlags != 0;
}
}
public void EndianSwap()
{
LicenseeId = LicenseeId.EndianSwap();
LicenseBits = LicenseBits.EndianSwap();
LicenseFlags = LicenseFlags.EndianSwap();
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct XCONTENT_HEADER
{
public const uint kSignatureTypeConBE = 0x434F4E20;
public const uint kSignatureTypeLiveBE = 0x4C495645;
public const uint kSignatureTypePirsBE = 0x50495253;
public uint SignatureType;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x228)]
public byte[] Signature;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)]
public XCONTENT_LICENSEE[] LicenseDescriptors;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x14)]
public byte[] ContentId;
public uint SizeOfHeaders;
public XE_CONSOLE_SIGNATURE ConsoleSignature
{
get
{
var sig = Utility.BytesToStruct<XE_CONSOLE_SIGNATURE>(Signature);
sig.EndianSwap();
return sig;
}
}
public string SignatureTypeString
{
get
{
switch(SignatureType)
{
case kSignatureTypeConBE:
return "CON";
case kSignatureTypeLiveBE:
return "LIVE";
case kSignatureTypePirsBE:
return "PIRS";
}
return SignatureType.ToString("X8");
}
}
public void EndianSwap()
{
SignatureType = SignatureType.EndianSwap();
for (int i = 0; i < 0x10; i++)
LicenseDescriptors[i].EndianSwap();
SizeOfHeaders = SizeOfHeaders.EndianSwap();
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)]
public struct UCHAR_80
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x100)]
public byte[] Bytes;
public string String
{
get
{
return Encoding.Unicode.GetString(Bytes).Trim(new char[] { '\0' });
}
}
public void EndianSwap()
{
for (int i = 0; i < Bytes.Length; i += 2)
Array.Reverse(Bytes, i, 2);
}
public override string ToString()
{
return String;
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)]
public struct UCHAR_40
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x80)]
public byte[] Bytes;
public string String
{
get
{
return Encoding.Unicode.GetString(Bytes).Trim(new char[] { '\0' });
}
}
public void EndianSwap()
{
for (int i = 0; i < Bytes.Length; i += 2)
Array.Reverse(Bytes, i, 2);
}
public override string ToString()
{
return String;
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct VERSION
{
public byte MajorMinor;
public ushort Build;
public byte QFE;
public bool IsValid
{
get
{
return MajorMinor != 0 || Build != 0 || QFE != 0;
}
}
public override string ToString()
{
return $"{(MajorMinor >> 4) & 0xF}.{MajorMinor & 0xF}.{Build}.{QFE}";
}
public void EndianSwap()
{
Build = Build.EndianSwap();
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct XEX2_EXECUTION_ID
{
public uint MediaId;
public VERSION Version;
public VERSION BaseVersion;
public uint TitleId;
public byte Platform;
public byte ExecutableType;
public byte DiscNum;
public byte DiscsInSet;
public uint SaveGameId;
public void EndianSwap()
{
MediaId = MediaId.EndianSwap();
Version.EndianSwap();
BaseVersion.EndianSwap();
TitleId = TitleId.EndianSwap();
SaveGameId = SaveGameId.EndianSwap();
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)]
public struct XCONTENT_METADATA
{
public void EndianSwap()
{
ContentType = ContentType.EndianSwap();
ContentMetadataVersion = ContentMetadataVersion.EndianSwap();
ContentSize = ContentSize.EndianSwap();
ExecutionId.EndianSwap();
Creator = Creator.EndianSwap();
StfsVolumeDescriptor.EndianSwap();
DataFiles = DataFiles.EndianSwap();
DataFilesSize = DataFilesSize.EndianSwap();
VolumeType = VolumeType.EndianSwap();
OnlineCreator = OnlineCreator.EndianSwap();
Category = Category.EndianSwap();
foreach (var str in DisplayName)
str.EndianSwap();
foreach (var str in Description)
str.EndianSwap();
Publisher.EndianSwap();
TitleName.EndianSwap();
ThumbnailSize = ThumbnailSize.EndianSwap();
foreach (var str in DisplayNameEx)
str.EndianSwap();
TitleThumbnailSize = TitleThumbnailSize.EndianSwap();
foreach (var str in DescriptionEx)
str.EndianSwap();
}
public uint ContentType;
public uint ContentMetadataVersion;
public ulong ContentSize;
public XEX2_EXECUTION_ID ExecutionId;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)]
public byte[] ConsoleId;
public ulong Creator;
public STF_VOLUME_DESCRIPTOR StfsVolumeDescriptor;
public uint DataFiles;
public ulong DataFilesSize;
public uint VolumeType;
public ulong OnlineCreator;
public uint Category;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x20)]
public byte[] Reserved2;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x24)]
public byte[] TypeSpecificData;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x14)]
public byte[] DeviceId;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 9)]
public UCHAR_80[] DisplayName;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 9)]
public UCHAR_80[] Description;
public UCHAR_40 Publisher;
public UCHAR_40 TitleName;
public byte FlagsAsBYTE;
public uint ThumbnailSize;
public uint TitleThumbnailSize;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x3D00)]
public byte[] Thumbnail;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public UCHAR_80[] DisplayNameEx;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x3D00)]
public byte[] TitleThumbnail;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public UCHAR_80[] DescriptionEx;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)]
public struct XCONTENT_METADATA_INSTALLER
{
public const uint kTypeSystemUpdate = 0x53555044; // SUPD
public const uint kTypeTitleUpdate = 0x54555044; // TUPD
public uint MetaDataType;
public VERSION CurrentVersion;
public VERSION NewVersion;
public bool IsSystemUpdate
{
get
{
return MetaDataType == kTypeSystemUpdate;
}
}
public bool IsTitleUpdate
{
get
{
return MetaDataType == kTypeTitleUpdate;
}
}
public bool IsValid
{
get
{
return MetaDataType == kTypeSystemUpdate || MetaDataType == kTypeTitleUpdate;
}
}
public void EndianSwap()
{
MetaDataType = MetaDataType.EndianSwap();
CurrentVersion.EndianSwap();
NewVersion.EndianSwap();
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct STF_DIRECTORY_ENTRY
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 40)]
public byte[] FileNameBytes;
public byte Flags;
public byte ValidDataBlocks0;
public byte ValidDataBlocks1;
public byte ValidDataBlocks2;
public byte AllocationBlocks0;
public byte AllocationBlocks1;
public byte AllocationBlocks2;
public byte FirstBlockNumber0;
public byte FirstBlockNumber1;
public byte FirstBlockNumber2;
public short DirectoryIndex;
public uint FileSize;
public int CreationTimeRaw;
public int LastWriteTimeRaw;
public DateTime CreationTime
{
get
{
return Utility.DecodeMSTime(CreationTimeRaw);
}
set
{
CreationTimeRaw = Utility.EncodeMSTime(value);
}
}
public DateTime LastWriteTime
{
get
{
return Utility.DecodeMSTime(LastWriteTimeRaw);
}
set
{
LastWriteTimeRaw = Utility.EncodeMSTime(value);
}
}
public byte FileNameLength
{
get
{
return (byte)(Flags & 0x3F);
}
set
{
Flags = (byte)(value & 0x3F);
}
}
public bool IsDirectory
{
get
{
return (Flags & 0x80) == 0x80;
}
}
public bool IsContiguous
{
get
{
return (Flags & 0x40) == 0x40;
}
}
public string FileName
{
get
{
return Encoding.ASCII.GetString(FileNameBytes, 0, FileNameLength).Trim(new char[] { '\0' });
}
set
{
FileNameLength = (byte)value.Length;
FileNameBytes = Encoding.ASCII.GetBytes(value);
if (FileNameBytes.Length > FileNameLength)
Array.Resize(ref FileNameBytes, FileNameLength);
}
}
public int ValidDataBlocks
{
get
{
return ValidDataBlocks0 | (ValidDataBlocks1 << 8) | (ValidDataBlocks2 << 16);
}
}
public int AllocationBlocks
{
get
{
return AllocationBlocks0 | (AllocationBlocks1 << 8) | (AllocationBlocks2 << 16);
}
}
public int FirstBlockNumber
{
get
{
return FirstBlockNumber0 | (FirstBlockNumber1 << 8) | (FirstBlockNumber2 << 16);
}
}
public bool IsValid
{
get
{
return Flags != 0 && FileNameLength > 0 && FileNameLength < 41 && FileName.IndexOfAny(StfsFileSystem.kInvalidFilenameChars) < 0;
}
}
public void EndianSwap()
{
DirectoryIndex = DirectoryIndex.EndianSwap();
FileSize = FileSize.EndianSwap();
CreationTimeRaw = CreationTimeRaw.EndianSwap();
LastWriteTimeRaw = LastWriteTimeRaw.EndianSwap();
}
public override string ToString()
{
return FileName;
}
}
// AKA "Josh" sector, at sector 4 / 0x800 of HDD
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct CACHE_PARTITION_DATA
{
public const uint kMagicJosh = 0x4A6F7368; // "Josh"
public uint CacheSignature;
public XE_CONSOLE_SIGNATURE Signature;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public STF_VOLUME_DESCRIPTOR[] VolumeDescriptor; // both are console-signed into the signature above
public uint Version;
public uint LastUsedIndex;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public uint[] TitleID;
public bool IsValid
{
get
{
return CacheSignature == kMagicJosh;
}
}
public void EndianSwap()
{
CacheSignature = CacheSignature.EndianSwap();
Signature.EndianSwap();
for (int i = 0; i < 2; i++)
VolumeDescriptor[i].EndianSwap();
Version = Version.EndianSwap();
LastUsedIndex = LastUsedIndex.EndianSwap();
for (int i = 0; i < 2; i++)
TitleID[i] = TitleID[i].EndianSwap();
}
}
}