summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/KernelKit/DriveMgr.h
blob: 9d46170335eb32abbbf3bef1e39df2197ab225b6 (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
/* -------------------------------------------

	Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved.

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

#ifndef INC_DRIVE_MANAGER_H
#define INC_DRIVE_MANAGER_H

#include <KernelKit/UserProcessScheduler.h>
#include <CompilerKit/CompilerKit.h>
#include <KernelKit/DebugOutput.h>
#include <KernelKit/DeviceMgr.h>
#include <KernelKit/KPC.h>
#include <NewKit/Defines.h>
#include <NewKit/KString.h>
#include <NewKit/Ref.h>

#define kDriveMaxCount	(4U)
#define kDriveSectorSz	(512U)
#define kDriveInvalidID (-1)
#define kDriveNameLen	(32)

#define drv_sector_cnt(SIZE, SECTOR_SZ) (((SIZE) + (SECTOR_SZ)) / (SECTOR_SZ))

namespace Kernel
{
	enum
	{
		kInvalidDrive = -1,

		/// Storage types, combine with flags.
		kBlockDevice	  = 0xAD,
		kMassStorageDrive = 0xDA,
		kFloppyDrive	  = 0xCD,
		kOpticalDrive	  = 0xDC, // CD-ROM/DVD-ROM/Blu-Ray
		kTapeDrive		  = 0xD7,

		/// Storage flags, combine with types.
		kReadOnlyDrive	  = 0x10, // Read only drive
		kEPMDrive		  = 0x11, // Explicit Partition Map.
		kEPTDrive		  = 0x12, // ESP w/ EPM partition.
		kMBRDrive		  = 0x13, // PC classic partition scheme
		kGPTDrive		  = 0x14, // PC new partition scheme
		kUnformattedDrive = 0x15,
		kStorageCount	  = 9,
	};

	/// @brief Media drive trait type.
	struct DriveTrait final
	{
		Char  fName[kDriveNameLen]; // /System, /boot, //./Devices/USB...
		Int32 fKind;				// fMassStorage, fFloppy, fOpticalDrive.
		Int32 fFlags;				// fReadOnly, fEPMDrive...

		/// @brief Packet drive (StorageKit compilant.)
		struct DrivePacket final
		{
			VoidPtr fPacketContent{nullptr};			//! packet body.
			Char	fPacketMime[kDriveNameLen] = "*/*"; //! identify what we're sending.
			SizeT	fPacketSize{0UL};					//! packet size
			UInt32	fPacketCRC32{0UL};					//! sanity crc, in case if good is set to false
			Boolean fPacketGood{YES};
			Lba		fPacketLba{0UL};
			Boolean fPacketReadOnly{NO};
		} fPacket;

		Lba	  fLbaStart{0}, fLbaEnd{0};
		SizeT fSectorSz{512};

		Void (*fInput)(DrivePacket packet);
		Void (*fOutput)(DrivePacket packet);
		Void (*fVerify)(DrivePacket packet);
		Void (*fInit)(DrivePacket packet);
		const Char* (*fDriveKind)(Void);
	};

	///! drive as a device.
	typedef DriveTrait* DriveTraitPtr;

	/**
	 * @brief Mounted drives interface.
	 * @note This class has all of it's drive set to nullptr, allocate them using
	 * GetAddressOf(index).
	 */
	class MountpointInterface final
	{
	public:
		explicit MountpointInterface() = default;
		~MountpointInterface()		   = default;

		NE_COPY_DEFAULT(MountpointInterface);

	public:
		DriveTrait& A()
		{
			return mA;
		}

		DriveTrait& B()
		{
			return mB;
		}

		DriveTrait& C()
		{
			return mC;
		}

		DriveTrait& D()
		{
			return mD;
		}

		enum
		{
			kDriveIndexA = 0,
			kDriveIndexB,
			kDriveIndexC,
			kDriveIndexD,
			kDriveIndexInvalid,
		};

		DriveTraitPtr GetAddressOf(const Int32& index)
		{
			err_local_get() = kErrorSuccess;

			switch (index)
			{
			case kDriveIndexA:
				return &mA;
			case kDriveIndexB:
				return &mB;
			case kDriveIndexC:
				return &mC;
			case kDriveIndexD:
				return &mD;
			default: {
				err_local_get() = kErrorNoSuchDisk;
				kout << "No such disc letter.\n";

				break;
			}
			}

			return nullptr;
		}

	private:
		DriveTrait mA, mB, mC, mD;
	};

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

	/// @brief Gets the drive kind (ATA, SCSI, AHCI...)
	/// @param void none.
	/// @return the drive kind (ATA, Flash, NVM)
	const Char* io_drv_kind(Void);

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

	/// @brief Fetches the main drive.
	/// @return the new drive as a trait.
	DriveTrait io_construct_main_drive(Void) noexcept;

	namespace Detect
	{
		Void io_detect_drive(DriveTrait& trait);
	}

	/// @brief Read from newfs disk.
	/// @param Mnt mounted interface.
	/// @param DrvTrait drive info
	/// @param DrvIndex drive index.
	/// @return
	Int32 fs_ifs_read(MountpointInterface* Mnt, DriveTrait& DrvTrait, Int32 DrvIndex);

	/// @brief Write to ifs disk.
	/// @param Mnt mounted interface.
	/// @param DrvTrait drive info
	/// @param DrvIndex drive index.
	/// @return
	Int32 fs_ifs_write(MountpointInterface* Mnt, DriveTrait& DrvTrait, Int32 DrvIndex);
} // namespace Kernel

#endif /* ifndef INC_DRIVE_MANAGER_H */