]> FriiDump Source - friidump.git/blob - libfriidump/xbox_ref/utils.h
FriiDump 0.5.3.10: add Redump evidence and optimize Xbox flow
[friidump.git] / libfriidump / xbox_ref / utils.h
1 #ifndef UTILS_H
2 #define UTILS_H
3
4 #include <windows.h>
5 #include <stdint.h>
6 #include "xbe_cert.h"
7
8 #define SECTORS_PER_READ 128 // 128 * 2048 = 256KB (The 8163B has a 256KB hardware cache.)
9 #define MAX_RETRIES 3        // How many times to retry a failed block
10
11 // Xbox DVD layout constants
12 //
13 // REDUMP_SECTORS is the unlocked game/XISO view length reported by the
14 // GDR-8050L after successful authentication.  It is not the full Redump-style
15 // reconstructed disc-image length.
16 #define REDUMP_SECTORS 3431264
17
18 // Full Original Xbox/XGD1 Redump-style 2048-byte sector image length.
19 // This includes video L0, pre-game padding, the full unlocked game/XISO view,
20 // post-game padding, and video L1.  Non-LBA structures such as PFI/DMI/lead-in
21 // remain sidecar/profile data, not bytes inside the ISO stream.
22 #define XGD1_FULL_REDUMP_SECTORS 3820880
23 #define XGD1_GAME_OUTPUT_START_LBA 198144
24 #define XGD1_XISO_LEADIN_SECTORS 32
25 #define XGD1_GAME_SOURCE_START_LBA 32
26 #define XGD1_GAME_SOURCE_SECTORS (REDUMP_SECTORS - XGD1_XISO_LEADIN_SECTORS)
27 #define XGD1_VIDEO_TOTAL_SECTORS 6992
28 #define XGD1_VIDEO_L0_SECTORS 6832
29 #define XGD1_VIDEO_L1_SECTORS 160
30 #define XGD1_VIDEO_L1_OUTPUT_START_LBA (XGD1_FULL_REDUMP_SECTORS - XGD1_VIDEO_L1_SECTORS)
31
32 // Redump/XGD1 physical layer-break from the Redump reference page.
33 // This is metadata for the reconstructed 2048-byte-sector ISO image.
34 #define XGD1_REDUMP_LAYER_BREAK_LBA 1913776
35
36 // Compatibility alias used by existing code paths that refer to the output
37 // layer break.  Do not confuse this with the GDR-8050L unlocked game-view
38 // sector count (3431264).
39 #define LAYER_BREAK XGD1_REDUMP_LAYER_BREAK_LBA
40 #define LAYER_THRESHOLD 2000000
41
42 // Legacy contiguous/hybrid layout start used by earlier experiments.
43 #define START_LBA_MAGIC 306112
44
45 // Handle cases where stdbool.h isn't available
46 #ifndef __cplusplus
47 typedef int bool;
48 #define true 1
49 #define false 0
50 #endif
51
52 #pragma pack(push, 1)
53 typedef struct _XDFS_SUPERBLOCK
54 {
55     char Magic[20];         // Should be "Microsoft Xbox Disk"
56     uint32_t RootDirSector; // Sector where the root directory starts
57     uint32_t RootDirSize;   // Size of the root directory in bytes
58     FILETIME ImageTime;     // Creation time
59     char Unused[0x7C8];     // Padding to 2048 bytes
60 } XDFS_SUPERBLOCK;
61
62 typedef struct _XDFS_DIRENTRY
63 {
64     uint16_t LeftEntry;  // Offset to left node (it's a B-Tree)
65     uint16_t RightEntry; // Offset to right node
66     uint32_t Sector;     // Starting sector of the file/dir
67     uint32_t Size;       // Size of the file/dir
68     uint8_t Attributes;  // Directory, Hidden, etc.
69     uint8_t NameLength;  // Length of name
70     char Name[1];        // The name (variable length)
71 } XDFS_DIRENTRY;
72 #pragma pack(pop)
73
74 #pragma pack(push, 1) // Ensure no padding is added by the compiler
75 typedef struct
76 {
77     // Offset: 0x00 (0)
78     char     Identifier[20];   // "MICROSOFT*XBOX*MEDIA"
79     
80     // Offset: 0x14 (20)
81     uint32_t RootLBA;          // LBA of the Root Directory Table (Relative to Partition Start)
82     
83     // Offset: 0x18 (24)
84     uint32_t RootSize;         // Size of the Root Directory Table in bytes
85     
86     // Offset: 0x1C (28)
87     uint32_t VolumeSize;       // Total Number of Sectors in this Partition (The 2.11GB value)
88     
89     // Offset: 0x20 (32)
90     uint64_t VolumeTimestamp;  // 64-bit Windows FileTime
91     
92     // Offset: 0x28 (40)
93     // The remainder of the 2048-byte sector is reserved/padding (2048 - 40 = 2008)
94     uint8_t  Reserved[2008];  
95 } XDFS_VOLUME_DESCRIPTOR;
96 #pragma pack(pop)
97
98 // XDFS Directory Entry Structure
99 typedef struct
100 {
101     uint16_t LeftChild;  // Offset / 4
102     uint16_t RightChild; // Offset / 4
103     uint32_t StartLBA;   // File Data LBA
104     uint32_t FileSize;   // File Size in Bytes
105     uint8_t Attributes;
106     uint8_t FileNameLength;
107     char FileName[1]; // Variable length
108 } XDFS_DIR_ENTRY;
109
110 HANDLE OpenDrive(char driveLetter);
111 void CloseDrive(HANDLE hDevice);
112 int IsDiscPresent(HANDLE hDevice);
113 void ControlTray(HANDLE hDevice, BOOL eject);
114 BOOL TestUnitReady(HANDLE hDevice);
115 BOOL StartDriveUnit(HANDLE hDevice);
116 BOOL EnsureDriveReady(HANDLE hDevice, DWORD timeoutMs);
117 BOOL StopDriveUnit(HANDLE hDevice);
118 void AutomateTrayCycle(HANDLE hDevice);
119 BOOL SetDriveSpeedMax(HANDLE hDevice);
120 void ForceMediaRefresh(HANDLE hDevice);
121 void HexDump(unsigned char* buffer, uint32_t size);
122 void outputdata(const uint8_t* buf, uint32_t lines);
123 uint8_t chksum8(const unsigned char *buff, size_t len);
124 void FormatElapsedTime(DWORD dwMilliseconds, char* outStr);
125 void PrintFormattedCapacity(unsigned char* scsibuffer);
126 void ListOpticalDrives(void);
127 uint32_t GetTotalSectors(HANDLE hDevice);
128 uint32_t GetXboxPhysicalSectors(HANDLE hDevice);
129 void RefreshVolume(HANDLE hDevice);
130 void ListDirectoryRecursive(HANDLE hDevice, uint32_t lba, uint32_t size, int level);
131 void ReadXboxGameDir(HANDLE hDevice);
132 void SanitizeFilename(char* filename);
133 BOOL ScsiReadSectors(HANDLE hDevice, uint32_t lba, uint16_t count, unsigned char* buffer);
134 XboxGameInfo GetXboxGameInfo(HANDLE hDevice);
135 void DisplayXboxGameInfo(XboxGameInfo info);
136 void DisplayXboxRating(uint32_t ratings);
137 void GetMediaID(HANDLE hDevice, char* outMediaId);
138 void PrintGamePartitionHash(const char* filename);
139 void GetDiscMetadata(HANDLE hDevice, uint32_t* totalSectors, bool* isDualLayer, XboxGameInfo *gameInfo);
140 uint32_t GetGamePartitionSize(HANDLE hDevice, uint32_t totalDiscSectors, XDFS_VOLUME_DESCRIPTOR *vol);
141 struct xbox_ref_dump_result_s;
142 BOOL DumpXboxGameDisc(HANDLE hDevice, const char *filename, char xisoFormat, uint32_t totalDiscSectors, bool isDualLayer, bool EjectOnSuccess, struct xbox_ref_dump_result_s *result);
143
144 #endif