summaryrefslogtreecommitdiffhomepage
path: root/src/kernel/FSKit/Ext2.h
blob: 9523df31558976d6e5997e184efb46d3f8a83538 (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 2024-2025, Amlal El Mahrouss (amlal@nekernel.org)
// Licensed under the Apache License, Version 2.0 (see LICENSE file)
// Official repository: https://github.com/nekernel-org/nekernel

#ifndef FSKIT_EXT2_H
#define FSKIT_EXT2_H

#include <CompilerKit/CompilerKit.h>
#include <KernelKit/DriveMgr.h>
#include <NeKit/Config.h>
#include <NeKit/KString.h>
#include <hint/CompilerHint.h>

/// @file Ext2.h
/// @brief EXT2 filesystem structures, constants, and base wrappers.

/// EXT2 Constants
#define kExt2FSMagic (0xEF53)
#define kExt2FSMaxFileNameLen (255U)
#define kExt2FSSuperblockOffset (1024)
#define kExt2FSRootInodeNumber (2)
#define kExt2FSInodeSize (128U)
#define kExt2FSBlockSizeBase (1024U)

#define kExt2FSRev0 (0)
#define kExt2FSRev1 (1)

/// EXT2 file types
enum {
  kExt2FileTypeUnknown      = 0,
  kExt2FileTypeRegular      = 1,
  kExt2FileTypeDirectory    = 2,
  kExt2FileTypeCharDevice   = 3,
  kExt2FileTypeBlockDevice  = 4,
  kExt2FileTypeFIFO         = 5,
  kExt2FileTypeSocket       = 6,
  kExt2FileTypeSymbolicLink = 7
};

typedef struct EXT2_GROUP_DESCRIPTOR final {
  UInt32 fBlockBitmap;
  UInt32 fInodeBitmap;
  UInt32 fInodeTable;
  UInt16 fFreeBlocksCount;
  UInt16 fFreeInodesCount;
  UInt16 fBgUsedDirsCount;
  UInt16 fBgPad;
  UInt32 fBgReserved[3];
} EXT2_GROUP_DESCRIPTOR;

struct PACKED EXT2_SUPER_BLOCK final {
  Kernel::UInt32 fInodeCount;
  Kernel::UInt32 fBlockCount;
  Kernel::UInt32 fReservedBlockCount;
  Kernel::UInt32 fFreeBlockCount;
  Kernel::UInt32 fFreeInodeCount;
  Kernel::UInt32 fFirstDataBlock;
  Kernel::UInt32 fLogBlockSize;
  Kernel::UInt32 fLogFragmentSize;
  Kernel::UInt32 fBlocksPerGroup;
  Kernel::UInt32 fFragmentsPerGroup;
  Kernel::UInt32 fInodesPerGroup;
  Kernel::UInt32 fMountTime;
  Kernel::UInt32 fWriteTime;
  Kernel::UInt16 fMountCount;
  Kernel::UInt16 fMaxMountCount;
  Kernel::UInt16 fMagic;
  Kernel::UInt16 fState;
  Kernel::UInt16 fErrors;
  Kernel::UInt16 fMinorRevision;
  Kernel::UInt32 fLastCheck;
  Kernel::UInt32 fCheckInterval;
  Kernel::UInt32 fCreatorOS;
  Kernel::UInt32 fRevisionLevel;
  Kernel::UInt16 fDefaultUID;
  Kernel::UInt16 fDefaultGID;

  // EXT2_DYNAMIC_REV fields
  Kernel::UInt32 fFirstInode;
  Kernel::UInt16 fInodeSize;
  Kernel::UInt16 fBlockGroupNumber;
  Kernel::UInt32 fFeatureCompat;
  Kernel::UInt32 fFeatureIncompat;
  Kernel::UInt32 fFeatureROCompat;
  Kernel::UInt8  fUUID[16];
  Kernel::Char   fVolumeName[16];
  Kernel::Char   fLastMounted[64];
  Kernel::UInt32 fAlgoBitmap;

  Kernel::UInt8  fPreallocBlocks;
  Kernel::UInt8  fPreallocDirBlocks;
  Kernel::UInt16 fReservedGDTBlocks;

  Kernel::UInt8  fJournalUUID[16];
  Kernel::UInt32 fJournalInode;
  Kernel::UInt32 fJournalDevice;
  Kernel::UInt32 fLastOrphan;

  Kernel::UInt32 fHashSeed[4];
  Kernel::UInt8  fDefHashVersion;
  Kernel::UInt8  fReservedCharPad;
  Kernel::UInt16 fReservedWordPad;
  Kernel::UInt32 fDefaultMountOpts;
  Kernel::UInt32 fFirstMetaBlockGroup;

  Kernel::UInt8 fReserved[760];  // Padding to make 1024 bytes
};

struct PACKED EXT2_INODE final {
  Kernel::UInt16 fMode;
  Kernel::UInt16 fUID;
  Kernel::UInt32 fSize;
  Kernel::UInt32 fAccessTime;
  Kernel::UInt32 fCreateTime;
  Kernel::UInt32 fModifyTime;
  Kernel::UInt32 fDeleteTime;
  Kernel::UInt16 fGID;
  Kernel::UInt16 fLinksCount;
  Kernel::UInt32 fBlocks;
  Kernel::UInt32 fFlags;
  Kernel::UInt32 fOSD1;

  Kernel::UInt32 fBlock[15];  // direct 0-11, indirect 12, double 13, triple 14

  Kernel::UInt32 fGeneration;
  Kernel::UInt32 fFileACL;
  Kernel::UInt32 fDirACL;
  Kernel::UInt32 fFragmentAddr;

  Kernel::UInt8 fOSD2[12];
};

/// Directory entry
struct PACKED EXT2_DIR_ENTRY final {
  Kernel::UInt32 fInode;
  Kernel::UInt16 fRecordLength;
  Kernel::UInt8  fNameLength;
  Kernel::UInt8  fFileType;
  Kernel::Char   fName[kExt2FSMaxFileNameLen];
};

/// VFS usage
struct Ext2Node {
  Kernel::UInt32 inodeNumber;
  EXT2_INODE     inode;
  Kernel::UInt32 cursor{0};
};

#endif