NeKernel dev
Loading...
Searching...
No Matches
Ext2+IFS.h
Go to the documentation of this file.
1/* ========================================
2
3 Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.
4
5======================================== */
6
7#pragma once
8
9#include <FSKit/Ext2.h>
10#include <KernelKit/DriveMgr.h>
11#include <KernelKit/HeapMgr.h>
12#include <KernelKit/KPC.h>
13#include <NeKit/ErrorOr.h>
14#include <NeKit/KernelPanic.h>
15#include <NeKit/Utils.h>
16
17namespace Kernel {
19class Ext2Context final {
20 public:
21 DriveTrait* drive{nullptr};
23
26
29 if (superblock) {
31 superblock = nullptr;
32 }
33 }
34
35 Ext2Context(const Ext2Context&) = delete;
37
39 other.drive = nullptr;
40 other.superblock = nullptr;
41 }
42
44 if (this != &other) {
45 if (superblock) {
47 }
48 drive = other.drive;
49 superblock = other.superblock;
50 other.drive = nullptr;
51 other.superblock = nullptr;
52 }
53 return *this;
54 }
55
56 SizeT BlockSize() const {
58 return kExt2FSBlockSizeBase << superblock->fLogBlockSize;
59 }
60
61 operator BOOL() { return superblock != nullptr; }
62};
63
67
69 Kernel::UInt32 size) {
70 if (!drv || !buffer) return false;
71
73 pkt.fPacketContent = buffer;
74 pkt.fPacketSize = size;
75 pkt.fPacketLba = lba;
76 drv->fInput(pkt);
77
78 return pkt.fPacketGood;
79}
80
82 Kernel::UInt32 size) {
83 if (!drv || !buffer) return false;
84
86 pkt.fPacketContent = const_cast<VoidPtr>(buffer);
87 pkt.fPacketSize = size;
88 pkt.fPacketLba = lba;
89 drv->fOutput(pkt);
90 return pkt.fPacketGood;
91}
92
95
96 auto buf = Kernel::mm_alloc_ptr(sizeof(EXT2_SUPER_BLOCK), true, false);
98
100
101 if (!ext2_read_block(ctx->drive, blockLba, buf, sizeof(EXT2_SUPER_BLOCK))) {
104 }
105
106 auto sb = reinterpret_cast<EXT2_SUPER_BLOCK*>(buf);
107 if (sb->fMagic != kExt2FSMagic) {
110 }
111
112 ctx->superblock = sb;
114}
115
116// Load inode
119
120 auto nodePtr = Kernel::mm_alloc_ptr(sizeof(Ext2Node), true, false);
122
123 auto ext2Node = reinterpret_cast<Ext2Node*>(nodePtr);
124 ext2Node->inodeNumber = inodeNumber;
125
126 // Compute block group and index within group
127 Kernel::UInt32 inodesPerGroup = ctx->superblock->fInodesPerGroup;
128 Kernel::UInt32 group = (inodeNumber - 1) / inodesPerGroup;
129
130 // dummy: just offset first inode
131 Kernel::UInt32 inodeTableBlock = ctx->superblock->fFirstInode + group;
132
133 if (!ext2_read_block(ctx->drive, inodeTableBlock, &ext2Node->inode, sizeof(EXT2_INODE))) {
134 Kernel::mm_free_ptr(nodePtr);
136 }
137
138 ext2Node->cursor = 0;
139 return Kernel::ErrorOr<Ext2Node*>(ext2Node);
140}
141
142/*
143 * Ext2FileSystemParser Class
144 *
145 * Provides high-level interface for EXT2 filesystem operations
146 */
148 private:
149 Ext2Context fCtx; // Internal EXT2 context
150
151 public:
152 /*
153 * Constructor
154 * Initializes the parser with a drive interface
155 *
156 * @param drive: Pointer to drive trait for disk I/O operations
157 */
159
161
162 /*
163 * Open a file or directory by path
164 *
165 * @param path: Full path to the file/directory (e.g., "/home/user/file.txt")
166 * @param restrict_type: Access mode restriction (e.g., "r", "w", "rw")
167 * @return: VoidPtr handle to the opened file/directory, or nullptr on failure
168 */
169 VoidPtr Open(const char* path, const char* restrict_type);
170
171 /*
172 * Read data from an open file node
173 *
174 * @param node: File node handle returned by Open()
175 * @param flags: Read operation flags
176 * @param size: Number of bytes to read
177 * @return: Pointer to allocated buffer containing read data, or nullptr on failure
178 * Caller is responsible for freeing the returned buffer
179 */
180 VoidPtr Read(VoidPtr node, Int32 flags, SizeT size);
181
182 /*
183 * Write data to an open file node
184 *
185 * @param node: File node handle returned by Open()
186 * @param data: Buffer containing data to write
187 * @param flags: Write operation flags
188 * @param size: Number of bytes to write
189 */
190 Void Write(VoidPtr node, VoidPtr data, Int32 flags, SizeT size);
191
192 /*
193 * Seek to a specific position in the file
194 *
195 * @param node: File node handle
196 * @param offset: Byte offset from beginning of file
197 * @return: true on success, false on failure
198 */
199 BOOL Seek(VoidPtr node, SizeT offset);
200
201 /*
202 * Get current position in the file
203 *
204 * @param node: File node handle
205 * @return: Current byte offset from beginning of file
206 */
208
209 /*
210 * Reset file position to beginning
211 *
212 * @param node: File node handle
213 * @return: true on success, false on failure
214 */
216
217 /*
218 * Read data from a named file within a directory node
219 *
220 * @param name: Name of file within the directory
221 * @param node: Directory node handle
222 * @param flags: Read operation flags
223 * @param size: Number of bytes to read
224 * @return: Pointer to allocated buffer containing read data, or nullptr on failure
225 */
226 VoidPtr Read(const char* name, VoidPtr node, Int32 flags, SizeT size);
227
228 /*
229 * Write data to a named file within a directory node
230 *
231 * @param name: Name of file within the directory
232 * @param node: Directory node handle
233 * @param data: Buffer containing data to write
234 * @param flags: Write operation flags
235 * @param size: Number of bytes to write
236 */
237 Void Write(const char* name, VoidPtr node, VoidPtr data, Int32 flags, SizeT size);
238
239 /*
240 * Create a new regular file
241 *
242 * @param path: Full path for the new file
243 * @return: VoidPtr handle to the created file, or nullptr on failure
244 */
245 VoidPtr Create(const char* path);
246
247 /*
248 * Create a new directory
249 *
250 * @param path: Full path for the new directory
251 * @return: VoidPtr handle to the created directory, or nullptr on failure
252 */
253 VoidPtr CreateDirectory(const char* path);
254
255 /*
256 * Close and free a file/directory node
257 * Note: This method is not shown in the implementation but would typically be needed
258 *
259 * @param node: Node handle to close and free
260 */
262
263 /*
264 * Get file/directory information
265 * Note: This method is not shown in the implementation but would typically be needed
266 *
267 * @param node: Node handle
268 * @param info: Structure to fill with file information (size, type, permissions, etc.)
269 * @return: true on success, false on failure
270 */
272};
273} // namespace Kernel
#define NE_COPY_DELETE(KLASS)
Definition Detail.h:13
Drive Manager.
#define BOOL
EXT2 filesystem structures, constants, and base wrappers.
#define kExt2FSMagic
EXT2 Constants.
Definition Ext2.h:19
#define kExt2FSSuperblockOffset
Definition Ext2.h:21
#define kExt2FSBlockSizeBase
Definition Ext2.h:24
: Memory allocation support for the NeKernel.
Kernel Procedure Code.
ErrorOr class for error handling.
Definition ErrorOr.h:22
Context for an EXT2 filesystem on a given drive.
Definition Ext2+IFS.h:19
Ext2Context & operator=(const Ext2Context &)=delete
~Ext2Context()
Clean up.
Definition Ext2+IFS.h:28
Ext2Context(Kernel::DriveTrait *drv)
context with a drive
Definition Ext2+IFS.h:25
Ext2Context(const Ext2Context &)=delete
EXT2_SUPER_BLOCK * superblock
Definition Ext2+IFS.h:22
Ext2Context & operator=(Ext2Context &&other)
Definition Ext2+IFS.h:43
SizeT BlockSize() const
Definition Ext2+IFS.h:56
Ext2Context(Ext2Context &&other)
Definition Ext2+IFS.h:38
DriveTrait * drive
Definition Ext2+IFS.h:21
SizeT Tell(VoidPtr node)
Void Write(VoidPtr node, VoidPtr data, Int32 flags, SizeT size)
BOOL Seek(VoidPtr node, SizeT offset)
VoidPtr Create(const char *path)
Void Close(VoidPtr node)
BOOL Rewind(VoidPtr node)
Ext2FileSystemParser(DriveTrait *drive)
Ext2Context fCtx
Definition Ext2+IFS.h:149
VoidPtr Read(VoidPtr node, Int32 flags, SizeT size)
VoidPtr Open(const char *path, const char *restrict_type)
BOOL GetInfo(VoidPtr node, VoidPtr info)
VoidPtr CreateDirectory(const char *path)
UPS inline definitions.
Definition Device.h:12
BOOL ext2_read_block(Kernel::DriveTrait *drv, Kernel::UInt32 lba, VoidPtr buffer, Kernel::UInt32 size)
Definition Ext2+IFS.h:68
void Void
Definition Config.h:87
constexpr KPCError kErrorHeapOutOfMemory
Definition KPC.h:44
__SIZE_TYPE__ SizeT
Definition Config.h:60
Kernel::ErrorOr< EXT2_SUPER_BLOCK * > ext2_load_superblock(Ext2Context *ctx)
Definition Ext2+IFS.h:93
Int32 mm_free_ptr(VoidPtr heap_ptr)
Declare pointer as free.
Definition HeapMgr.cc:187
__UINT32_TYPE__ UInt32
Definition Config.h:44
void * VoidPtr
Definition Config.h:33
constexpr KPCError kErrorInvalidData
Definition KPC.h:56
__INT32_TYPE__ Int32
Definition Config.h:38
BOOL ext2_write_block(Kernel::DriveTrait *drv, Kernel::UInt32 lba, const VoidPtr buffer, Kernel::UInt32 size)
Definition Ext2+IFS.h:81
VoidPtr mm_alloc_ptr(SizeT sz, Bool wr, Bool user, SizeT pad_amount=0)
Allocate chunk of memory.
Definition HeapMgr.cc:101
constexpr KPCError kErrorDisk
Definition KPC.h:55
Kernel::ErrorOr< Ext2Node * > ext2_load_inode(Ext2Context *ctx, Kernel::UInt32 inodeNumber)
Definition Ext2+IFS.h:117
Definition Ext2.h:110
Definition Ext2.h:52
Kernel::UInt32 fInodesPerGroup
Definition Ext2.h:63
Kernel::UInt32 fFirstInode
Definition Ext2.h:80
VFS usage.
Definition Ext2.h:144
Kernel::UInt32 inodeNumber
Definition Ext2.h:145
Packet drive (StorageKit compilant.).
Definition DriveMgr.h:60
SizeT fPacketSize
identify what we're sending.
Definition DriveMgr.h:63
VoidPtr fPacketContent
Definition DriveMgr.h:61
Boolean fPacketGood
sanity crc, in case if good is set to false
Definition DriveMgr.h:65
Lba fPacketLba
Definition DriveMgr.h:66
Media drive trait type.
Definition DriveMgr.h:54
SizeT fSectorSz
Definition DriveMgr.h:71
Void(* fOutput)(DrivePacket &packet)
Definition DriveMgr.h:74
Void(* fInput)(DrivePacket &packet)
Definition DriveMgr.h:73