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
|
/* -------------------------------------------
Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.
------------------------------------------- */
/**
* @file BootATA.cc
* @author Amlal El Mahrouss (amlal@nekernel.org)
* @brief ATA driver.
* @version 0.1
* @date 2024-02-02
*
* @copyright Copyright (c) Amlal El Mahrouss
*
*/
#include <BootKit/BootKit.h>
#include <BootKit/HW/ATA.h>
#include <FirmwareKit/EFI.h>
#define kATADataLen (256)
/// bugs: 0
using namespace Boot;
static Boolean kATADetected = false;
static UInt16 kATAData[kATADataLen] = {0};
Boolean boot_ata_detected(Void);
STATIC Boolean boot_ata_wait_io(UInt16 IO) {
for (int i = 0; i < 400; i++) rt_in8(IO + ATA_REG_STATUS);
ATAWaitForIO_Retry:
auto status_rdy = rt_in8(IO + ATA_REG_STATUS);
if ((status_rdy & ATA_SR_BSY)) goto ATAWaitForIO_Retry;
ATAWaitForIO_Retry2:
status_rdy = rt_in8(IO + ATA_REG_STATUS);
if (status_rdy & ATA_SR_ERR) return false;
if (!(status_rdy & ATA_SR_DRDY)) goto ATAWaitForIO_Retry2;
return true;
}
Void boot_ata_select(UInt16 Bus) {
if (Bus == ATA_PRIMARY_IO)
rt_out8(Bus + ATA_REG_HDDEVSEL, ATA_PRIMARY_SEL);
else
rt_out8(Bus + ATA_REG_HDDEVSEL, ATA_SECONDARY_SEL);
}
Boolean boot_ata_init(UInt16 Bus, UInt8 Drive, UInt16& OutBus, UInt8& OutMaster) {
NE_UNUSED(Drive);
if (boot_ata_detected()) return true;
BootTextWriter writer;
UInt16 IO = Bus;
boot_ata_select(IO);
// Bus init, NEIN bit.
rt_out8(IO + ATA_REG_NEIN, 1);
// identify until it's good.
ATAInit_Retry:
auto status_rdy = rt_in8(IO + ATA_REG_STATUS);
if (status_rdy & ATA_SR_ERR) {
writer.Write(L"BootZ: ATA: Not an IDE based drive.\r");
return false;
}
if ((status_rdy & ATA_SR_BSY)) goto ATAInit_Retry;
rt_out8(IO + ATA_REG_COMMAND, ATA_CMD_IDENTIFY);
/// fetch serial info
/// model, speed, number of sectors...
boot_ata_wait_io(IO);
for (SizeT indexData = 0ul; indexData < kATADataLen; ++indexData) {
kATAData[indexData] = Kernel::HAL::rt_in16(IO + ATA_REG_DATA);
}
OutBus = (Bus == ATA_PRIMARY_IO) ? BootDeviceATA::kPrimary : BootDeviceATA::kSecondary;
OutMaster = (Bus == ATA_PRIMARY_IO) ? ATA_MASTER : ATA_SLAVE;
// Why? the current disk driver writes whole word instead of a single byte (expected btw) so i'm
// planning to finish +Next drivers for 0.0.3
return NO;
}
Void boot_ata_read(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeASCII* Buf, SizeT SectorSz,
SizeT Size) {
Lba /= SectorSz;
UInt8 Command = ((!Master) ? 0xE0 : 0xF0);
boot_ata_wait_io(IO);
boot_ata_select(IO);
rt_out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F));
rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + SectorSz) / SectorSz));
rt_out8(IO + ATA_REG_LBA0, (Lba) & 0xFF);
rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8);
rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16);
rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24);
rt_out8(IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO);
boot_ata_wait_io(IO);
for (SizeT IndexOff = 0; IndexOff < Size; ++IndexOff) {
boot_ata_wait_io(IO);
Buf[IndexOff] = Kernel::HAL::rt_in16(IO + ATA_REG_DATA);
boot_ata_wait_io(IO);
}
}
Void boot_ata_write(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeASCII* Buf, SizeT SectorSz,
SizeT Size) {
Lba /= SectorSz;
UInt8 Command = ((!Master) ? 0xE0 : 0xF0);
boot_ata_wait_io(IO);
boot_ata_select(IO);
rt_out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F));
rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + (SectorSz)) / SectorSz));
rt_out8(IO + ATA_REG_LBA0, (Lba) & 0xFF);
rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8);
rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16);
rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24);
rt_out8(IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO);
boot_ata_wait_io(IO);
for (SizeT IndexOff = 0; IndexOff < Size; ++IndexOff) {
boot_ata_wait_io(IO);
rt_out16(IO + ATA_REG_DATA, Buf[IndexOff]);
boot_ata_wait_io(IO);
}
boot_ata_wait_io(IO);
}
/// @check is ATA detected?
Boolean boot_ata_detected(Void) {
return kATADetected;
}
/***
*
*
* @brief ATA Device class.
*
*
*/
/**
* @brief ATA Device constructor.
* @param void none.
*/
BootDeviceATA::BootDeviceATA() noexcept {
if (boot_ata_init(ATA_PRIMARY_IO, true, this->Leak().mBus, this->Leak().mMaster) ||
boot_ata_init(ATA_SECONDARY_IO, true, this->Leak().mBus, this->Leak().mMaster)) {
kATADetected = true;
}
}
/**
* @brief Is ATA detected?
*/
BootDeviceATA::operator bool() {
return boot_ata_detected();
}
/**
@brief Read Buf from disk
@param Sz Sector size
@param Buf buffer
*/
BootDeviceATA& BootDeviceATA::Read(CharacterTypeASCII* Buf, SizeT SectorSz) {
if (!boot_ata_detected()) {
Leak().mErr = true;
return *this;
}
this->Leak().mErr = false;
if (!Buf || SectorSz < 1) return *this;
boot_ata_read(this->Leak().mBase, this->Leak().mBus, this->Leak().mMaster, Buf, SectorSz,
this->Leak().mSize);
return *this;
}
/**
@brief Write Buf into disk
@param Sz Sector size
@param Buf buffer
*/
BootDeviceATA& BootDeviceATA::Write(CharacterTypeASCII* Buf, SizeT SectorSz) {
if (!boot_ata_detected()) {
Leak().mErr = true;
return *this;
}
Leak().mErr = false;
if (!Buf || SectorSz < 1 || this->Leak().mSize < 1) {
Leak().mErr = true;
return *this;
}
boot_ata_write(this->Leak().mBase, this->Leak().mBus, this->Leak().mMaster, Buf, SectorSz,
this->Leak().mSize);
return *this;
}
/**
* @brief ATA trait getter.
* @return BootDeviceATA::ATATrait& the drive config.
*/
BootDeviceATA::ATATrait& BootDeviceATA::Leak() {
return mTrait;
}
/***
@brief Getter, gets the number of sectors inside the drive.
*/
SizeT BootDeviceATA::GetSectorsCount() noexcept {
return (kATAData[61] << 16) | kATAData[60];
}
SizeT BootDeviceATA::GetDiskSize() noexcept {
return this->GetSectorsCount() * BootDeviceATA::kSectorSize;
}
|