ReactOS 0.4.16-dev-2615-g89221f5
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
25/* For disk.c!DiskError() */
29{
30#if 0 // TODO: ATA/IDE error code descriptions.
31 switch (ErrorCode)
32 {
33 default: return "Unknown error code";
34 }
35#endif
36 return NULL;
37}
38
39/* FUNCTIONS ******************************************************************/
40
41static
42VOID
44{
45 UCHAR DetectedCount, UnitNumber;
46
49
50 /* Find first HDD and CD */
51 AtaInit(&DetectedCount);
52 for (UnitNumber = 0; UnitNumber < DetectedCount; UnitNumber++)
53 {
54 PDEVICE_UNIT DeviceUnit = AtaGetDevice(UnitNumber);
55 if (!DeviceUnit)
56 continue;
57
58 if (DeviceUnit->Flags & ATA_DEVICE_ATAPI)
59 {
60 if (!CdDrive)
61 CdDrive = DeviceUnit;
62 }
63 else
64 {
65 if (!HardDrive)
66 HardDrive = DeviceUnit;
67 }
68 }
69}
70
71static inline
74{
75 /* Xbox has only 1 IDE controller and no floppy */
76 if (DriveNumber < 0x80 || (DriveNumber & 0x0F) >= 2)
77 return NULL;
78
79 if (!AtaInitialized)
81
82 /* HDD */
83 if ((DriveNumber == 0x80) && HardDrive)
84 return HardDrive;
85
86 /* CD */
87 if (((DriveNumber & 0xF0) > 0x80) && CdDrive)
88 return CdDrive;
89
90 return NULL;
91}
92
94{
95 WARN("DiskResetController(0x%x) DISK OPERATION FAILED -- RESETTING CONTROLLER\n", DriveNumber);
96 /* No-op on XBOX */
97 return TRUE;
98}
99
102 _In_ UCHAR DriveNumber)
103{
104 PDEVICE_UNIT DeviceUnit;
105
106 DeviceUnit = XboxDiskDriveNumberToDeviceUnit(DriveNumber);
107 if (!DeviceUnit)
108 return -1; // MaximumType;
109
110 if (DeviceUnit == CdDrive) // (DeviceUnit->Flags & ATA_DEVICE_ATAPI)
111 return CdromController;
112 else // if (DeviceUnit == HardDrive)
113 return DiskPeripheral;
114}
115
116// FIXME: Dummy for entry.S/linux.S
118{
119 /* No-op on XBOX */
120}
121
124 IN UCHAR DriveNumber,
125 IN ULONGLONG SectorNumber,
128{
129 PDEVICE_UNIT DeviceUnit;
131
132 TRACE("XboxDiskReadLogicalSectors() DriveNumber: 0x%x SectorNumber: %I64u SectorCount: %u Buffer: 0x%x\n",
133 DriveNumber, SectorNumber, SectorCount, Buffer);
134
135 DeviceUnit = XboxDiskDriveNumberToDeviceUnit(DriveNumber);
136 if (!DeviceUnit)
137 return FALSE;
138
139 Success = AtaReadLogicalSectors(DeviceUnit, SectorNumber, SectorCount, Buffer);
140 if (!Success)
141 DiskError("Disk Read Failed", -1);
142 return Success;
143}
144
147{
148 PDEVICE_UNIT DeviceUnit;
149
150 TRACE("XboxDiskGetDriveGeometry(0x%x)\n", DriveNumber);
151
152 DeviceUnit = XboxDiskDriveNumberToDeviceUnit(DriveNumber);
153 if (!DeviceUnit)
154 return FALSE;
155
156 Geometry->Cylinders = DeviceUnit->Cylinders;
157 Geometry->Heads = DeviceUnit->Heads;
158 Geometry->SectorsPerTrack = DeviceUnit->SectorsPerTrack;
159 Geometry->BytesPerSector = DeviceUnit->SectorSize;
160 Geometry->Sectors = DeviceUnit->TotalSectors;
161
162 return TRUE;
163}
164
165ULONG
167{
168 PDEVICE_UNIT DeviceUnit;
169
170 DeviceUnit = XboxDiskDriveNumberToDeviceUnit(DriveNumber);
171 if (!DeviceUnit)
172 return 1; // Unknown count.
173
174 /*
175 * If LBA is supported then the block size will be 64 sectors (32k).
176 * If not then the block size is the size of one track.
177 */
178 if (DeviceUnit->Flags & ATA_DEVICE_LBA)
179 return 64;
180 else
181 return DeviceUnit->SectorsPerTrack;
182}
183
184/* EOF */
unsigned char BOOLEAN
Definition: actypes.h:127
#define WARN(fmt,...)
Definition: precomp.h:61
VOID DiskError(_In_ PCSTR ErrorString, _In_ ULONG ErrorCode)
Definition: disk.c:48
#define DBG_DEFAULT_CHANNEL(ch)
Definition: debug.h:106
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:1270
BOOLEAN AtaInit(_Out_ PUCHAR DetectedCount)
Definition: hwide.c:1279
BOOLEAN AtaReadLogicalSectors(_In_ PDEVICE_UNIT DeviceUnit, _In_ ULONG64 SectorNumber, _In_ ULONG SectorCount, _Out_writes_bytes_all_(SectorCount *DeviceUnit->SectorSize) PVOID Buffer)
Definition: hwide.c:1230
#define ATA_DEVICE_LBA
Definition: hwide.h:31
#define ATA_DEVICE_ATAPI
Definition: hwide.h:30
#define ASSERT(a)
Definition: mode.c:44
_In_ NDIS_ERROR_CODE ErrorCode
Definition: ndis.h:4436
#define _In_
Definition: no_sal2.h:158
ULONG SectorCount
Definition: part_brfr.c:22
@ 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:24
ULONG BytesPerSector
Number of bytes per sector.
Definition: disk.h:28
ULONG Cylinders
Number of cylinders on the disk.
Definition: disk.h:25
ULONGLONG Sectors
Total number of disk sectors/LBA blocks.
Definition: disk.h:29
ULONG SectorsPerTrack
Number of sectors per track.
Definition: disk.h:27
ULONG Heads
Number of heads on the disk.
Definition: disk.h:26
unsigned char UCHAR
Definition: typedefs.h:53
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
BOOLEAN DiskResetController(UCHAR DriveNumber)
Definition: xboxdisk.c:93
static BOOLEAN AtaInitialized
Definition: xboxdisk.c:21
PCSTR DiskGetErrorCodeString(_In_ ULONG ErrorCode)
Definition: xboxdisk.c:27
static VOID XboxDiskInit(VOID)
Definition: xboxdisk.c:43
static PDEVICE_UNIT XboxDiskDriveNumberToDeviceUnit(UCHAR DriveNumber)
Definition: xboxdisk.c:73
BOOLEAN XboxDiskReadLogicalSectors(IN UCHAR DriveNumber, IN ULONGLONG SectorNumber, IN ULONG SectorCount, OUT PVOID Buffer)
Definition: xboxdisk.c:123
ULONG XboxDiskGetCacheableBlockCount(UCHAR DriveNumber)
Definition: xboxdisk.c:166
VOID __cdecl DiskStopFloppyMotor(VOID)
Definition: xboxdisk.c:117
BOOLEAN XboxDiskGetDriveGeometry(UCHAR DriveNumber, PGEOMETRY Geometry)
Definition: xboxdisk.c:146
CONFIGURATION_TYPE DiskGetConfigType(_In_ UCHAR DriveNumber)
Definition: xboxdisk.c:101
static PDEVICE_UNIT CdDrive
Definition: xboxdisk.c:20