summaryrefslogtreecommitdiffhomepage
path: root/dev/Kernel/src/DriveMgr.cc
blob: 097023517776bc3f7ad842859eb91cea61184874 (plain)
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
/* -------------------------------------------

	Copyright (C) 2024, t& Labs, all rights reserved.

------------------------------------------- */

#include <KernelKit/DebugOutput.h>
#include <KernelKit/DriveMgr.h>
#include <NewKit/Utils.h>
#include <FirmwareKit/EPM.h>
#include <Mod/ATA/ATA.h>
#include <Mod/AHCI/AHCI.h>
#include <Mod/NVME/NVME.h>

/***********************************************************************************/
/// @file DriveMgr.cc
/// @brief Drive Manager of minoskrnl.
/***********************************************************************************/

namespace Kernel
{
#if defined(__ATA_PIO__) || defined(__ATA_DMA__)
	STATIC UInt16 kATAIO	 = 0U;
	STATIC UInt8  kATAMaster = 0U;
#endif

	/// @brief reads from an ATA drive.
	/// @param pckt Packet structure (fPacketContent must be non null)
	/// @return
	Void io_drv_input(DriveTrait::DrivePacket* pckt)
	{
		if (!pckt || !pckt->fPacketDrive)
		{
			return;
		}

		if (!StringBuilder::Equals("fs/detect-packet", pckt->fPacketMime) &&
			pckt->fPacketDrive->fLbaStart > 0 && pckt->fPacketDrive->fLbaEnd > 0)
		{
			if (pckt->fPacketLba > pckt->fPacketDrive->fLbaEnd)
			{
				pckt->fPacketGood = NO;
				return;
			}
			else if (pckt->fPacketLba < pckt->fPacketDrive->fLbaStart)
			{
				pckt->fPacketGood = NO;
				return;
			}
		}

#ifdef __AHCI__
		drv_std_read(pckt->fPacketLba, (Char*)pckt->fPacketContent, pckt->fPacketDrive->fSectorSz, pckt->fPacketSize);
#elif defined(__ATA_PIO__) || defined(__ATA_DMA__)
		drv_std_read(pckt->fPacketLba, kATAIO, kATAMaster, (Char*)pckt->fPacketContent, pckt->fPacketDrive->fSectorSz, pckt->fPacketSize);
#endif
	}

	/// @brief Writes to an ATA drive.
	/// @param pckt the packet to write.
	/// @return
	Void io_drv_output(DriveTrait::DrivePacket* pckt)
	{
		if (!pckt || !pckt->fPacketDrive)
		{
			return;
		}

		if (pckt->fPacketReadOnly)
		{
			pckt->fPacketGood = NO;
			return;
		}

		if (!StringBuilder::Equals("fs/detect-packet", pckt->fPacketMime) &&
			pckt->fPacketDrive->fLbaStart > 0 && pckt->fPacketDrive->fLbaEnd > 0)
		{
			if (pckt->fPacketLba > pckt->fPacketDrive->fLbaEnd)
			{
				pckt->fPacketGood = NO;
				return;
			}
			else if (pckt->fPacketLba < pckt->fPacketDrive->fLbaStart)
			{
				pckt->fPacketGood = NO;
				return;
			}
		}

#ifdef __AHCI__
		drv_std_write(pckt->fPacketLba, (Char*)pckt->fPacketContent, pckt->fPacketDrive->fSectorSz, pckt->fPacketSize);
#elif defined(__ATA_PIO__) || defined(__ATA_DMA__)
		drv_std_write(pckt->fPacketLba, kATAIO, kATAMaster, (Char*)pckt->fPacketContent, pckt->fPacketDrive->fSectorSz, pckt->fPacketSize);
#endif
	}

	/// @brief Executes a disk check on the ATA drive.
	/// @param pckt the packet to read.
	/// @return
	Void io_drv_init(DriveTrait::DrivePacket* pckt)
	{
		if (!pckt)
		{
			return;
		}

#if defined( __ATA_PIO__ ) || defined( __ATA_DMA__ )
		kATAMaster = 0;
		kATAIO	   = 0;
#endif

#if defined(__ATA_PIO__) || defined(__ATA_DMA__)
		kATAMaster = true;
		kATAIO	   = ATA_PRIMARY_IO;

		if (drv_std_init(kATAIO, kATAMaster, kATAIO, kATAMaster))
		{
			pckt->fPacketGood = YES;
			return;
		}

		kATAMaster = false;
		kATAIO	   = ATA_SECONDARY_IO;

		if (!drv_std_init(kATAIO, kATAMaster, kATAIO, kATAMaster))
		{
			return;
		}

		pckt->fPacketGood = YES;
#elif defined(__AHCI__)
		UInt16 pi = 0;

		if (!drv_std_init(pi))
		{
			return;
		}
#endif // if defined(__ATA_PIO__) || defined (__ATA_DMA__)
	}

/// @brief Gets the drive kind (ATA, SCSI, AHCI...)
/// @param no arguments.
/// @return no arguments.
#ifdef __ATA_PIO__
	const Char* io_drv_kind(Void)
	{
		return "ATA-PIO";
	}
#endif
#ifdef __ATA_DMA__
	const Char* io_drv_kind(Void)
	{
		return "ATA-DMA";
	}
#endif
#ifdef __AHCI__
	const Char* io_drv_kind(Void)
	{
		return "AHCI";
	}
#endif
#ifdef __ZKA_MINIMAL_OS__
	const Char* io_drv_kind(Void)
	{
		return "Not Loaded";
	}
#endif

	/// @brief Unimplemented drive function.
	/// @param pckt the packet to read.
	/// @return
	Void io_drv_unimplemented(DriveTrait::DrivePacket* pckt) noexcept
	{
		ZKA_UNUSED(pckt);
	}

	/// @brief Makes a new drive.
	/// @return the new blank drive.
	DriveTrait io_construct_blank_drive() noexcept
	{
		DriveTrait trait;

		rt_copy_memory((VoidPtr) "/media/nul", trait.fName, rt_string_len("/media/nul"));
		trait.fKind = kInvalidDisc;

		trait.fInput	 = io_drv_unimplemented;
		trait.fOutput	 = io_drv_unimplemented;
		trait.fVerify	 = io_drv_unimplemented;
		trait.fInit		 = io_drv_unimplemented;
		trait.fDriveKind = io_drv_kind;

		kcout << "Construct: " << trait.fName << "\r";

		return trait;
	}

	namespace Detect
	{
		Void io_detect_drive(DriveTrait& trait)
		{
			EPM_PART_BLOCK block_struct;

			trait.fPacket.fPacketDrive = &trait;

#ifdef __ATA_PIO__
			trait.fSectorSz = kATASectorSize;
#elif defined(__AHCI__)
			trait.fSectorSz = kAHCISectorSize;
#else
			trait.fSectorSz = 512;
#endif
			trait.fPacket.fPacketLba	 = kEPMBootBlockLba;
			trait.fPacket.fPacketSize	 = sizeof(EPM_PART_BLOCK);
			trait.fPacket.fPacketContent = &block_struct;

			rt_copy_memory((VoidPtr) "fs/detect-packet", trait.fPacket.fPacketMime,
						   rt_string_len("fs/detect-packet"));

			trait.fInit(&trait.fPacket);

			trait.fInput(&trait.fPacket);

			if (rt_string_cmp(((BOOT_BLOCK_STRUCT*)trait.fPacket.fPacketContent)->Magic, kEPMMagic, kEPMMagicLength) == 0)
			{
				trait.fPacket.fPacketReadOnly = NO;
				trait.fKind					  = kMassStorageDisc | kEPMDrive;

				kcout << "Formatted Disk is EPM (Mass Storage)\r";

				trait.fSectorSz = block_struct.SectorSz;
				trait.fLbaEnd	= block_struct.LbaEnd;
				trait.fLbaStart = block_struct.LbaStart;

				if (trait.fSectorSz == 0)
				{
					ke_panic(RUNTIME_CHECK_FAILED, "Invalid EPM partition!");
				}
			}
			else
			{
				trait.fPacket.fPacketReadOnly = YES;
				trait.fKind					  = kMassStorageDisc | kUnformattedDrive | kReadOnlyDrive;

				kcout << "Scheme Found: " << block_struct.Name << endl;

				if (block_struct.Name[0] == 0)
					kcout << "Disk partition is unknown (Read Only)\r";
			}

			rt_copy_memory((VoidPtr) "*/*", trait.fPacket.fPacketMime,
						   rt_string_len("*/*"));

			trait.fPacket.fPacketLba	 = 0;
			trait.fPacket.fPacketSize	 = 0UL;
			trait.fPacket.fPacketContent = nullptr;
		}
	} // namespace Detect

	/// @brief Fetches the main drive.
	/// @return the new drive. (returns kEPMDrive if EPM formatted)
	DriveTrait io_construct_main_drive() noexcept
	{
		DriveTrait trait;

		constexpr auto kMainDrive = "/media/sda";

		rt_copy_memory((VoidPtr)kMainDrive, trait.fName, rt_string_len(kMainDrive));

		MUST_PASS(trait.fName[0] != 0);

		trait.fVerify	 = io_drv_unimplemented;
		trait.fOutput	 = io_drv_output;
		trait.fInput	 = io_drv_input;
		trait.fInit		 = io_drv_init;
		trait.fDriveKind = io_drv_kind;

		kcout << "Detecting partition scheme of: " << trait.fName << ".\r";

		Detect::io_detect_drive(trait);

		return trait;
	}
} // namespace Kernel