ReactOS 0.4.16-dev-2104-gb84fa49
xboxdisk.c
Go to the documentation of this file.
1/*
2 * PROJECT: FreeLoader
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Xbox specific disk access routines
5 * COPYRIGHT: Copyright 2004 Gé van Geldorp (gvg@reactos.com)
6 * Copyright 2019-2025 Dmitry Borisov (di.sean@protonmail.com)
7 */
8
9/* INCLUDES *******************************************************************/
10
11#include <freeldr.h>
12#include <hwide.h>
13
14#include <debug.h>
16
17/* GLOBALS ********************************************************************/
18
22
23/* DISK IO ERROR SUPPORT *****************************************************/
24
25static LONG lReportError = 0; // >= 0: display errors; < 0: hide errors.
26
28{
29 /* Set the reference count */
30 if (bShowError) ++lReportError;
31 else --lReportError;
32 return lReportError;
33}
34
35#if 0 // TODO: ATA/IDE error code descriptions.
37{
38 switch (ErrorCode)
39 {
40 default: return "unknown error code";
41 }
42}
43#endif
44
45static VOID DiskError(PCSTR ErrorString, ULONG ErrorCode)
46{
47 CHAR ErrorCodeString[200];
48
49 if (lReportError < 0)
50 return;
51
52#if 0 // TODO: ATA/IDE error code descriptions.
53 sprintf(ErrorCodeString, "%s\n\nError Code: 0x%lx\nError: %s",
55#else
57 sprintf(ErrorCodeString, "%s", ErrorString);
58#endif
59
60 ERR("%s\n", ErrorCodeString);
61 UiMessageBox(ErrorCodeString);
62}
63
64/* FUNCTIONS ******************************************************************/
65
66static
67VOID
69{
70 UCHAR DetectedCount;
71 UCHAR UnitNumber;
72 PDEVICE_UNIT DeviceUnit = NULL;
73
75
77
78 /* Find first HDD and CD */
79 AtaInit(&DetectedCount);
80 for (UnitNumber = 0; UnitNumber <= DetectedCount; UnitNumber++)
81 {
82 DeviceUnit = AtaGetDevice(UnitNumber);
83 if (DeviceUnit)
84 {
85 if (DeviceUnit->Flags & ATA_DEVICE_ATAPI)
86 {
87 if (!CdDrive)
88 CdDrive = DeviceUnit;
89 }
90 else
91 {
92 if (!HardDrive)
93 HardDrive = DeviceUnit;
94 }
95 }
96 }
97}
98
99static inline
102{
103 /* Xbox has only 1 IDE controller and no floppy */
104 if (DriveNumber < 0x80 || (DriveNumber & 0x0F) >= 2)
105 return NULL;
106
107 if (!AtaInitialized)
108 XboxDiskInit();
109
110 /* HDD */
111 if ((DriveNumber == 0x80) && HardDrive)
112 return HardDrive;
113
114 /* CD */
115 if (((DriveNumber & 0xF0) > 0x80) && CdDrive)
116 return CdDrive;
117
118 return NULL;
119}
120
122{
123 WARN("DiskResetController(0x%x) DISK OPERATION FAILED -- RESETTING CONTROLLER\n", DriveNumber);
124 /* No-op on XBOX */
125 return TRUE;
126}
127
130 _In_ UCHAR DriveNumber)
131{
132 PDEVICE_UNIT DeviceUnit;
133
134 DeviceUnit = XboxDiskDriveNumberToDeviceUnit(DriveNumber);
135 if (!DeviceUnit)
136 return -1; // MaximumType;
137
138 if (DeviceUnit == CdDrive) // (DeviceUnit->Flags & ATA_DEVICE_ATAPI)
139 return CdromController;
140 else // if (DeviceUnit == HardDrive)
141 return DiskPeripheral;
142}
143
144// FIXME: Dummy for entry.S/linux.S
146{
147 /* No-op on XBOX */
148}
149
152 IN UCHAR DriveNumber,
153 IN ULONGLONG SectorNumber,
156{
157 PDEVICE_UNIT DeviceUnit;
159
160 TRACE("XboxDiskReadLogicalSectors() DriveNumber: 0x%x SectorNumber: %I64u SectorCount: %u Buffer: 0x%x\n",
161 DriveNumber, SectorNumber, SectorCount, Buffer);
162
163 DeviceUnit = XboxDiskDriveNumberToDeviceUnit(DriveNumber);
164 if (!DeviceUnit)
165 return FALSE;
166
167 Success = AtaReadLogicalSectors(DeviceUnit, SectorNumber, SectorCount, Buffer);
168 if (!Success)
169 DiskError("Disk Read Failed", -1);
170 return Success;
171}
172
175{
176 PDEVICE_UNIT DeviceUnit;
177
178 TRACE("XboxDiskGetDriveGeometry(0x%x)\n", DriveNumber);
179
180 DeviceUnit = XboxDiskDriveNumberToDeviceUnit(DriveNumber);
181 if (!DeviceUnit)
182 return FALSE;
183
184 Geometry->Cylinders = DeviceUnit->Cylinders;
185 Geometry->Heads = DeviceUnit->Heads;
186 Geometry->SectorsPerTrack = DeviceUnit->SectorsPerTrack;
187 Geometry->BytesPerSector = DeviceUnit->SectorSize;
188 Geometry->Sectors = DeviceUnit->TotalSectors;
189
190 return TRUE;
191}
192
193ULONG
195{
196 PDEVICE_UNIT DeviceUnit;
197
198 DeviceUnit = XboxDiskDriveNumberToDeviceUnit(DriveNumber);
199 if (!DeviceUnit)
200 return 1; // Unknown count.
201
202 /*
203 * If LBA is supported then the block size will be 64 sectors (32k).
204 * If not then the block size is the size of one track.
205 */
206 if (DeviceUnit->Flags & ATA_DEVICE_LBA)
207 return 64;
208 else
209 return DeviceUnit->SectorsPerTrack;
210}
211
212/* EOF */
unsigned char BOOLEAN
#define WARN(fmt,...)
Definition: precomp.h:61
#define ERR(fmt,...)
Definition: precomp.h:57
#define DBG_DEFAULT_CHANNEL(ch)
Definition: debug.h:106
VOID UiMessageBox(_In_ PCSTR Format,...)
Definition: ui.c:359
Definition: bufpool.h:45
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define __cdecl
Definition: corecrt.h:121
@ Success
Definition: eventcreate.c:712
PDEVICE_UNIT AtaGetDevice(_In_ UCHAR UnitNumber)
Definition: hwide.c:1254
BOOLEAN AtaInit(_Out_ PUCHAR DetectedCount)
Definition: hwide.c:1264
BOOLEAN AtaReadLogicalSectors(_In_ PDEVICE_UNIT DeviceUnit, _In_ ULONG64 SectorNumber, _In_ ULONG SectorCount, _Out_writes_bytes_all_(SectorCount *DeviceUnit->SectorSize) PVOID Buffer)
Definition: hwide.c:1215
#define ATA_DEVICE_LBA
Definition: hwide.h:31
#define ATA_DEVICE_ATAPI
Definition: hwide.h:30
#define ASSERT(a)
Definition: mode.c:44
#define sprintf
Definition: sprintf.c:45
_In_ NDIS_ERROR_CODE ErrorCode
Definition: ndis.h:4436
#define _In_
Definition: no_sal2.h:158
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:329
ULONG SectorCount
Definition: part_xbox.c:31
static PCSTR DiskGetErrorCodeString(ULONG ErrorCode)
Definition: pcdisk.c:144
long LONG
Definition: pedump.c:60
@ DiskPeripheral
Definition: arc.h:138
@ CdromController
Definition: arc.h:128
enum _CONFIGURATION_TYPE CONFIGURATION_TYPE
#define TRACE(s)
Definition: solgame.cpp:4
Data structure for the ATA device.
Definition: hwide.h:12
ULONG Cylinders
Definition: hwide.h:14
ULONG SectorSize
Definition: hwide.h:23
ULONG SectorsPerTrack
Definition: hwide.h:20
ULONG64 TotalSectors
Definition: hwide.h:26
ULONG Flags
Definition: hwide.h:29
ULONG Heads
Definition: hwide.h:17
Definition: disk.h:26
ULONG BytesPerSector
Number of bytes per sector.
Definition: disk.h:30
ULONG Cylinders
Number of cylinders on the disk.
Definition: disk.h:27
ULONGLONG Sectors
Total number of disk sectors/LBA blocks.
Definition: disk.h:31
ULONG SectorsPerTrack
Number of sectors per track.
Definition: disk.h:29
ULONG Heads
Number of heads on the disk.
Definition: disk.h:28
const char * PCSTR
Definition: typedefs.h:52
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
uint64_t ULONGLONG
Definition: typedefs.h:67
#define OUT
Definition: typedefs.h:40
static PDEVICE_UNIT HardDrive
Definition: xboxdisk.c:19
static LONG lReportError
Definition: xboxdisk.c:25
BOOLEAN DiskResetController(UCHAR DriveNumber)
Definition: xboxdisk.c:121
static BOOLEAN AtaInitialized
Definition: xboxdisk.c:21
static VOID XboxDiskInit(VOID)
Definition: xboxdisk.c:68
static PDEVICE_UNIT XboxDiskDriveNumberToDeviceUnit(UCHAR DriveNumber)
Definition: xboxdisk.c:101
BOOLEAN XboxDiskReadLogicalSectors(IN UCHAR DriveNumber, IN ULONGLONG SectorNumber, IN ULONG SectorCount, OUT PVOID Buffer)
Definition: xboxdisk.c:151
ULONG XboxDiskGetCacheableBlockCount(UCHAR DriveNumber)
Definition: xboxdisk.c:194
VOID __cdecl DiskStopFloppyMotor(VOID)
Definition: xboxdisk.c:145
BOOLEAN XboxDiskGetDriveGeometry(UCHAR DriveNumber, PGEOMETRY Geometry)
Definition: xboxdisk.c:174
LONG DiskReportError(BOOLEAN bShowError)
Definition: xboxdisk.c:27
CONFIGURATION_TYPE DiskGetConfigType(_In_ UCHAR DriveNumber)
Definition: xboxdisk.c:129
static PDEVICE_UNIT CdDrive
Definition: xboxdisk.c:20
static VOID DiskError(PCSTR ErrorString, ULONG ErrorCode)
Definition: xboxdisk.c:45
unsigned char UCHAR
Definition: xmlstorage.h:181
char CHAR
Definition: xmlstorage.h:175