xref: /reactos/boot/freeldr/freeldr/include/fs/ext2.h (revision c9a20ced)
1 /*
2  *  FreeLoader
3  *  Copyright (C) 1998-2003  Brian Palmer  <brianp@sginet.com>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License along
16  *  with this program; if not, write to the Free Software Foundation, Inc.,
17  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #pragma once
21 
22 /*
23  *  grub/fs/ext2.c
24  *
25  *  GRUB  --  GRand Unified Bootloader
26  *  Copyright (C) 2003,2004,2005,2007  Free Software Foundation, Inc.
27  *
28  *  This program is free software; you can redistribute it and/or modify
29  *  it under the terms of the GNU General Public License as published by
30  *  the Free Software Foundation; either version 2 of the License, or
31  *  (at your option) any later version.
32  *
33  *  This program is distributed in the hope that it will be useful,
34  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
35  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36  *  GNU General Public License for more details.
37  *
38  *  You should have received a copy of the GNU General Public License
39  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
40  */
41 
42 /* Magic value used to identify an ext2 filesystem.  */
43 #define    EXT2_MAGIC        0xEF53
44 /* Amount of indirect blocks in an inode.  */
45 #define INDIRECT_BLOCKS        12
46 /* Maximum length of a pathname.  */
47 #define EXT2_PATH_MAX        4096
48 /* Maximum nesting of symlinks, used to prevent a loop.  */
49 #define    EXT2_MAX_SYMLINKCNT    8
50 
51 /* The good old revision and the default inode size.  */
52 #define EXT2_GOOD_OLD_REVISION        0
53 #define EXT2_DYNAMIC_REVISION        1
54 #define EXT2_GOOD_OLD_INODE_SIZE    128
55 
56 /* Filetype used in directory entry.  */
57 #define    FILETYPE_UNKNOWN    0
58 #define    FILETYPE_REG        1
59 #define    FILETYPE_DIRECTORY    2
60 #define    FILETYPE_SYMLINK    7
61 
62 /* Filetype information as used in inodes.  */
63 #define FILETYPE_INO_MASK    0170000
64 #define FILETYPE_INO_REG    0100000
65 #define FILETYPE_INO_DIRECTORY    0040000
66 #define FILETYPE_INO_SYMLINK    0120000
67 
68 /* The ext2 superblock.  */
69 struct ext2_sblock
70 {
71   ULONG total_inodes;
72   ULONG total_blocks;
73   ULONG reserved_blocks;
74   ULONG free_blocks;
75   ULONG free_inodes;
76   ULONG first_data_block;
77   ULONG log2_block_size;
78   LONG log2_fragment_size;
79   ULONG blocks_per_group;
80   ULONG fragments_per_group;
81   ULONG inodes_per_group;
82   ULONG mtime;
83   ULONG utime;
84   USHORT mnt_count;
85   USHORT max_mnt_count;
86   USHORT magic;
87   USHORT fs_state;
88   USHORT error_handling;
89   USHORT minor_revision_level;
90   ULONG lastcheck;
91   ULONG checkinterval;
92   ULONG creator_os;
93   ULONG revision_level;
94   USHORT uid_reserved;
95   USHORT gid_reserved;
96   ULONG first_inode;
97   USHORT inode_size;
98   USHORT block_group_number;
99   ULONG feature_compatibility;
100   ULONG feature_incompat;
101   ULONG feature_ro_compat;
102   ULONG unique_id[4];
103   char volume_name[16];
104   char last_mounted_on[64];
105   ULONG compression_info;
106   ULONG padding[77];
107 };
108 
109 /* The ext2 blockgroup.  */
110 struct ext2_block_group
111 {
112   ULONG block_id;
113   ULONG inode_id;
114   ULONG inode_table_id;
115   USHORT free_blocks;
116   USHORT free_inodes;
117   USHORT used_dirs;
118   USHORT pad;
119   ULONG reserved[3];
120 };
121 
122 /* The ext2 inode.  */
123 struct ext2_inode
124 {
125   USHORT mode;
126   USHORT uid;
127   ULONG size;
128   ULONG atime;
129   ULONG ctime;
130   ULONG mtime;
131   ULONG dtime;
132   USHORT gid;
133   USHORT nlinks;
134   ULONG blockcnt;  /* Blocks of 512 bytes!! */
135   ULONG flags;
136   ULONG osd1;
137   union
138   {
139     struct datablocks
140     {
141       ULONG dir_blocks[INDIRECT_BLOCKS];
142       ULONG indir_block;
143       ULONG double_indir_block;
144       ULONG tripple_indir_block;
145     } blocks;
146     char symlink[60];
147   };
148   ULONG version;
149   ULONG acl;
150   ULONG dir_acl;
151   ULONG fragment_addr;
152   ULONG osd2[3];
153 };
154 
155 /* The header of an ext2 directory entry.  */
156 #define EXT2_NAME_LEN 255
157 
158 struct ext2_dirent
159 {
160   ULONG inode;
161   USHORT direntlen;
162   UCHAR namelen;
163   UCHAR filetype;
164   CHAR name[EXT2_NAME_LEN];
165 };
166 
167 /*
168  * End of code from grub/fs/ext2.c
169  */
170 
171 typedef struct ext2_sblock        EXT2_SUPER_BLOCK, *PEXT2_SUPER_BLOCK;
172 typedef struct ext2_inode        EXT2_INODE, *PEXT2_INODE;
173 typedef struct ext2_block_group        EXT2_GROUP_DESC, *PEXT2_GROUP_DESC;
174 typedef struct ext2_dirent        EXT2_DIR_ENTRY, *PEXT2_DIR_ENTRY;
175 
176 /* Special inode numbers.  */
177 #define EXT2_ROOT_INO        2
178 
179 /* Feature set definitions.  */
180 #define EXT3_FEATURE_INCOMPAT_SUPP    0x0002
181 
182 /* Log2 size of ext2 block in bytes.  */
183 #define LOG2_BLOCK_SIZE(sb)    (sb->log2_block_size + 10)
184 
185 /* The size of an ext2 block in bytes.  */
186 #define EXT2_BLOCK_SIZE(sb)    (((SIZE_T)1) << LOG2_BLOCK_SIZE(sb))
187 
188 /* The revision level.  */
189 #define EXT2_REVISION(sb)    (sb->revision_level)
190 
191 /* The inode size.  */
192 #define EXT2_INODE_SIZE(sb)    (EXT2_REVISION(sb) == EXT2_GOOD_OLD_REVISION \
193                 ? EXT2_GOOD_OLD_INODE_SIZE \
194                 : sb->inode_size)
195 
196 #define EXT2_DESC_PER_BLOCK(s)    (EXT2_BLOCK_SIZE(s) / sizeof(struct ext2_block_group))
197 
198 // EXT2_INODE::mode values
199 #define EXT2_S_IRWXO    0x0007    // Other mask
200 #define EXT2_S_IXOTH    0x0001    // ---------x execute
201 #define EXT2_S_IWOTH    0x0002    // --------w- write
202 #define EXT2_S_IROTH    0x0004    // -------r-- read
203 
204 #define EXT2_S_IRWXG    0x0038    // Group mask
205 #define EXT2_S_IXGRP    0x0008    // ------x--- execute
206 #define EXT2_S_IWGRP    0x0010    // -----w---- write
207 #define EXT2_S_IRGRP    0x0020    // ----r----- read
208 
209 #define EXT2_S_IRWXU    0x01C0    // User mask
210 #define EXT2_S_IXUSR    0x0040    // ---x------ execute
211 #define EXT2_S_IWUSR    0x0080    // --w------- write
212 #define EXT2_S_IRUSR    0x0100    // -r-------- read
213 
214 #define EXT2_S_ISVTX    0x0200    // Sticky bit
215 #define EXT2_S_ISGID    0x0400    // SGID
216 #define EXT2_S_ISUID    0x0800    // SUID
217 
218 #define EXT2_S_IFMT        0xF000    // Format mask
219 #define EXT2_S_IFIFO    0x1000    // FIFO buffer
220 #define EXT2_S_IFCHR    0x2000    // Character device
221 #define EXT2_S_IFDIR    0x4000    // Directory
222 #define EXT2_S_IFBLK    0x6000    // Block device
223 #define EXT2_S_IFREG    0x8000    // Regular file
224 #define EXT2_S_IFLNK    0xA000    // Symbolic link
225 #define EXT2_S_IFSOCK    0xC000    // Socket
226 
227 #define FAST_SYMLINK_MAX_NAME_SIZE    60
228 
229 typedef struct _EXT2_VOLUME_INFO *PEXT2_VOLUME_INFO;
230 
231 typedef struct
232 {
233     ULONGLONG   FileSize;       // File size
234     ULONGLONG   FilePointer;    // File pointer
235     ULONG*      FileBlockList;  // File block list
236     EXT2_INODE  Inode;          // File's inode
237     PEXT2_VOLUME_INFO   Volume;
238 } EXT2_FILE_INFO, * PEXT2_FILE_INFO;
239 
240 const DEVVTBL* Ext2Mount(ULONG DeviceId);
241