summaryrefslogtreecommitdiffhomepage
path: root/Kernel/Sources/DriveManager.cxx
blob: 62195b9903760b339debdd7aab8df6bf6df9b9d3 (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
/* -------------------------------------------

	Copyright SoftwareLabs

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

#include <KernelKit/DebugOutput.hpp>
#include <KernelKit/DriveManager.hxx>
#include <Builtins/ATA/ATA.hxx>
#include <Builtins/AHCI/AHCI.hxx>
#include <NewKit/Utils.hpp>

/// @file DriveManager.cxx
/// @brief Kernel drive manager.

namespace NewOS
{
	static UInt16 kATAIO	 = 0U;
	static UInt8  kATAMaster = 0U;

	/// @brief reads from an ATA drive.
	/// @param pckt
	/// @return
	Void ke_drv_input(DriveTrait::DrivePacket* pckt)
	{
		if (!pckt)
		{
			return;
		}

		pckt->fPacketGood = false;

#ifdef __AHCI__
		drv_std_read(pckt->fLba, (Char*)pckt->fPacketContent, kAHCISectorSize, pckt->fPacketSize);
#elif defined(__ATA_PIO__) || defined(__ATA_DMA__)
		drv_std_read(pckt->fLba, kATAIO, kATAMaster, (Char*)pckt->fPacketContent, kATASectorSize, pckt->fPacketSize);
#endif

		pckt->fPacketGood = true;
	}

	/// @brief Writes to an ATA drive.
	/// @param pckt
	/// @return
	Void ke_drv_output(DriveTrait::DrivePacket* pckt)
	{
		if (!pckt)
		{
			return;
		}

		pckt->fPacketGood = false;

#ifdef __AHCI__
		drv_std_write(pckt->fLba, (Char*)pckt->fPacketContent, kATASectorSize, pckt->fPacketSize);
#elif defined(__ATA_PIO__) || defined(__ATA_DMA__)
		drv_std_write(pckt->fLba, kATAIO, kATAMaster, (Char*)pckt->fPacketContent, kATASectorSize, pckt->fPacketSize);
#endif

		pckt->fPacketGood = true;
	}

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

		pckt->fPacketGood = false;

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

		MUST_PASS(drv_std_init(kATAIO, kATAMaster, kATAIO, kATAMaster));
#endif // if defined(__ATA_PIO__) || defined (__ATA_DMA__)

		pckt->fPacketGood = true;
	}

/// @brief Gets the drive kind (ATA, SCSI, AHCI...)
/// @param
/// @return
#ifdef __ATA_PIO__
	const Char* ke_drive_kind(Void)
	{
		return "ATA-PIO";
	}
#endif

#ifdef __ATA_DMA__
	const Char* ke_drive_kind(Void)
	{
		return "ATA-DMA";
	}
#endif

#ifdef __AHCI__
	const Char* ke_drive_kind(Void)
	{
		return "AHCI";
	}
#endif

	/// @brief Unimplemented drive.
	/// @param pckt
	/// @return
	Void ke_drv_unimplemented(DriveTrait::DrivePacket* pckt)
	{
	}

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

		rt_copy_memory((VoidPtr) "/Mount/Null", trait.fName, rt_string_len("/Mount/Null"));
		trait.fKind = kInvalidDrive;

		trait.fInput	 = ke_drv_unimplemented;
		trait.fOutput	 = ke_drv_unimplemented;
		trait.fVerify	 = ke_drv_unimplemented;
		trait.fDriveKind = ke_drive_kind;

		return trait;
	}

	/// @brief Fetches the main drive.
	/// @return the new drive.
	DriveTrait construct_main_drive() noexcept
	{
		DriveTrait trait;

		rt_copy_memory((VoidPtr) "/Mount/MainDisk/", trait.fName, rt_string_len("/Mount/MainDisk/"));
		trait.fKind = kMassStorage | kEPMDrive;

		trait.fInput	 = ke_drv_input;
		trait.fOutput	 = ke_drv_output;
		trait.fVerify	 = ke_drv_check_disk;
		trait.fDriveKind = ke_drive_kind;

		return trait;
	}
} // namespace NewOS