0710196df09e8a644f43de042542efedccf07885
[reactos.git] / freeldr / freeldr / fs / ext2.c
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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <freeldr.h>
21 #include <fs.h>
22 #include "ext2.h"
23 #include <disk.h>
24 #include <rtl.h>
25 #include <ui.h>
26 #include <arch.h>
27 #include <mm.h>
28 #include <debug.h>
29 #include <cache.h>
30 #include <machine.h>
31
32
33 GEOMETRY Ext2DiskGeometry; // Ext2 file system disk geometry
34
35 PEXT2_SUPER_BLOCK Ext2SuperBlock = NULL; // Ext2 file system super block
36 PEXT2_GROUP_DESC Ext2GroupDescriptors = NULL; // Ext2 file system group descriptors
37
38 U8 Ext2DriveNumber = 0; // Ext2 file system drive number
39 U64 Ext2VolumeStartSector = 0; // Ext2 file system starting sector
40 U32 Ext2BlockSizeInBytes = 0; // Block size in bytes
41 U32 Ext2BlockSizeInSectors = 0; // Block size in sectors
42 U32 Ext2FragmentSizeInBytes = 0; // Fragment size in bytes
43 U32 Ext2FragmentSizeInSectors = 0; // Fragment size in sectors
44 U32 Ext2GroupCount = 0; // Number of groups in this file system
45 U32 Ext2InodesPerBlock = 0; // Number of inodes in one block
46 U32 Ext2GroupDescPerBlock = 0; // Number of group descriptors in one block
47
48 BOOL Ext2OpenVolume(U8 DriveNumber, U64 VolumeStartSector)
49 {
50
51 DbgPrint((DPRINT_FILESYSTEM, "Ext2OpenVolume() DriveNumber = 0x%x VolumeStartSector = %d\n", DriveNumber, VolumeStartSector));
52
53 // Store the drive number and start sector
54 Ext2DriveNumber = DriveNumber;
55 Ext2VolumeStartSector = VolumeStartSector;
56
57 if (!MachDiskGetDriveGeometry(DriveNumber, &Ext2DiskGeometry))
58 {
59 return FALSE;
60 }
61
62 //
63 // Initialize the disk cache for this drive
64 //
65 if (!CacheInitializeDrive(DriveNumber))
66 {
67 return FALSE;
68 }
69
70 // Read in the super block
71 if (!Ext2ReadSuperBlock())
72 {
73 return FALSE;
74 }
75
76 // Read in the group descriptors
77 if (!Ext2ReadGroupDescriptors())
78 {
79 return FALSE;
80 }
81
82 return TRUE;
83 }
84
85 /*
86 * Ext2OpenFile()
87 * Tries to open the file 'name' and returns true or false
88 * for success and failure respectively
89 */
90 FILE* Ext2OpenFile(PUCHAR FileName)
91 {
92 EXT2_FILE_INFO TempExt2FileInfo;
93 PEXT2_FILE_INFO FileHandle;
94 UCHAR SymLinkPath[EXT3_NAME_LEN];
95 UCHAR FullPath[EXT3_NAME_LEN * 2];
96 U32 Index;
97
98 DbgPrint((DPRINT_FILESYSTEM, "Ext2OpenFile() FileName = %s\n", FileName));
99
100 RtlZeroMemory(SymLinkPath, EXT3_NAME_LEN);
101
102 // Lookup the file in the file system
103 if (!Ext2LookupFile(FileName, &TempExt2FileInfo))
104 {
105 return NULL;
106 }
107
108 // If we got a symbolic link then fix up the path
109 // and re-call this function
110 if ((TempExt2FileInfo.Inode.i_mode & EXT2_S_IFMT) == EXT2_S_IFLNK)
111 {
112 DbgPrint((DPRINT_FILESYSTEM, "File is a symbolic link\n"));
113
114 // Now read in the symbolic link path
115 if (!Ext2ReadFile(&TempExt2FileInfo, TempExt2FileInfo.FileSize, NULL, SymLinkPath))
116 {
117 if (TempExt2FileInfo.FileBlockList != NULL)
118 {
119 MmFreeMemory(TempExt2FileInfo.FileBlockList);
120 }
121
122 return NULL;
123 }
124
125 DbgPrint((DPRINT_FILESYSTEM, "Symbolic link path = %s\n", SymLinkPath));
126
127 // Get the full path
128 if (SymLinkPath[0] == '/' || SymLinkPath[0] == '\\')
129 {
130 // Symbolic link is an absolute path
131 // So copy it to FullPath, but skip over
132 // the '/' char at the beginning
133 strcpy(FullPath, &SymLinkPath[1]);
134 }
135 else
136 {
137 // Symbolic link is a relative path
138 // Copy the first part of the path
139 strcpy(FullPath, FileName);
140
141 // Remove the last part of the path
142 for (Index=strlen(FullPath); Index>0; )
143 {
144 Index--;
145 if (FullPath[Index] == '/' || FullPath[Index] == '\\')
146 {
147 break;
148 }
149 }
150 FullPath[Index] = '\0';
151
152 // Concatenate the symbolic link
153 strcat(FullPath, Index == 0 ? "" : "/");
154 strcat(FullPath, SymLinkPath);
155 }
156
157 DbgPrint((DPRINT_FILESYSTEM, "Full file path = %s\n", FullPath));
158
159 if (TempExt2FileInfo.FileBlockList != NULL)
160 {
161 MmFreeMemory(TempExt2FileInfo.FileBlockList);
162 }
163
164 return Ext2OpenFile(FullPath);
165 }
166 else
167 {
168 FileHandle = MmAllocateMemory(sizeof(EXT2_FILE_INFO));
169
170 if (FileHandle == NULL)
171 {
172 if (TempExt2FileInfo.FileBlockList != NULL)
173 {
174 MmFreeMemory(TempExt2FileInfo.FileBlockList);
175 }
176
177 return NULL;
178 }
179
180 RtlCopyMemory(FileHandle, &TempExt2FileInfo, sizeof(EXT2_FILE_INFO));
181
182 return (FILE*)FileHandle;
183 }
184 }
185
186 /*
187 * Ext2LookupFile()
188 * This function searches the file system for the
189 * specified filename and fills in a EXT2_FILE_INFO structure
190 * with info describing the file, etc. returns true
191 * if the file exists or false otherwise
192 */
193 BOOL Ext2LookupFile(PUCHAR FileName, PEXT2_FILE_INFO Ext2FileInfoPointer)
194 {
195 int i;
196 U32 NumberOfPathParts;
197 UCHAR PathPart[261];
198 PVOID DirectoryBuffer;
199 U32 DirectoryInode = EXT3_ROOT_INO;
200 EXT2_INODE InodeData;
201 EXT2_DIR_ENTRY DirectoryEntry;
202
203 DbgPrint((DPRINT_FILESYSTEM, "Ext2LookupFile() FileName = %s\n", FileName));
204
205 RtlZeroMemory(Ext2FileInfoPointer, sizeof(EXT2_FILE_INFO));
206
207 //
208 // Figure out how many sub-directories we are nested in
209 //
210 NumberOfPathParts = FsGetNumPathParts(FileName);
211
212 //
213 // Loop once for each part
214 //
215 for (i=0; i<NumberOfPathParts; i++)
216 {
217 //
218 // Get first path part
219 //
220 FsGetFirstNameFromPath(PathPart, FileName);
221
222 //
223 // Advance to the next part of the path
224 //
225 for (; (*FileName != '\\') && (*FileName != '/') && (*FileName != '\0'); FileName++)
226 {
227 }
228 FileName++;
229
230 //
231 // Buffer the directory contents
232 //
233 if (!Ext2ReadDirectory(DirectoryInode, &DirectoryBuffer, &InodeData))
234 {
235 return FALSE;
236 }
237
238 //
239 // Search for file name in directory
240 //
241 if (!Ext2SearchDirectoryBufferForFile(DirectoryBuffer, (U32)Ext2GetInodeFileSize(&InodeData), PathPart, &DirectoryEntry))
242 {
243 MmFreeMemory(DirectoryBuffer);
244 return FALSE;
245 }
246
247 MmFreeMemory(DirectoryBuffer);
248
249 DirectoryInode = DirectoryEntry.inode;
250 }
251
252 if (!Ext2ReadInode(DirectoryInode, &InodeData))
253 {
254 return FALSE;
255 }
256
257 if (((InodeData.i_mode & EXT2_S_IFMT) != EXT2_S_IFREG) &&
258 ((InodeData.i_mode & EXT2_S_IFMT) != EXT2_S_IFLNK))
259 {
260 FileSystemError("Inode is not a regular file or symbolic link.");
261 return FALSE;
262 }
263
264 // Set the drive number
265 Ext2FileInfoPointer->DriveNumber = Ext2DriveNumber;
266
267 // If it's a regular file or a regular symbolic link
268 // then get the block pointer list otherwise it must
269 // be a fast symbolic link which doesn't have a block list
270 if (((InodeData.i_mode & EXT2_S_IFMT) == EXT2_S_IFREG) ||
271 ((InodeData.i_mode & EXT2_S_IFMT) == EXT2_S_IFLNK && InodeData.i_size > FAST_SYMLINK_MAX_NAME_SIZE))
272 {
273 Ext2FileInfoPointer->FileBlockList = Ext2ReadBlockPointerList(&InodeData);
274
275 if (Ext2FileInfoPointer->FileBlockList == NULL)
276 {
277 return FALSE;
278 }
279 }
280 else
281 {
282 Ext2FileInfoPointer->FileBlockList = NULL;
283 }
284
285 Ext2FileInfoPointer->FilePointer = 0;
286 Ext2FileInfoPointer->FileSize = Ext2GetInodeFileSize(&InodeData);
287 RtlCopyMemory(&Ext2FileInfoPointer->Inode, &InodeData, sizeof(EXT2_INODE));
288
289 return TRUE;
290 }
291
292 BOOL Ext2SearchDirectoryBufferForFile(PVOID DirectoryBuffer, U32 DirectorySize, PUCHAR FileName, PEXT2_DIR_ENTRY DirectoryEntry)
293 {
294 U32 CurrentOffset;
295 PEXT2_DIR_ENTRY CurrentDirectoryEntry;
296
297 DbgPrint((DPRINT_FILESYSTEM, "Ext2SearchDirectoryBufferForFile() DirectoryBuffer = 0x%x DirectorySize = %d FileName = %s\n", DirectoryBuffer, DirectorySize, FileName));
298
299 for (CurrentOffset=0; CurrentOffset<DirectorySize; )
300 {
301 CurrentDirectoryEntry = (PEXT2_DIR_ENTRY)(DirectoryBuffer + CurrentOffset);
302
303 if (CurrentDirectoryEntry->rec_len == 0)
304 {
305 break;
306 }
307
308 if ((CurrentDirectoryEntry->rec_len + CurrentOffset) > DirectorySize)
309 {
310 FileSystemError("Directory entry extends past end of directory file.");
311 return FALSE;
312 }
313
314 DbgPrint((DPRINT_FILESYSTEM, "Dumping directory entry at offset %d:\n", CurrentOffset));
315 DbgDumpBuffer(DPRINT_FILESYSTEM, CurrentDirectoryEntry, CurrentDirectoryEntry->rec_len);
316
317 if ((strnicmp(FileName, CurrentDirectoryEntry->name, CurrentDirectoryEntry->name_len) == 0) &&
318 (strlen(FileName) == CurrentDirectoryEntry->name_len))
319 {
320 RtlCopyMemory(DirectoryEntry, CurrentDirectoryEntry, sizeof(EXT2_DIR_ENTRY));
321
322 DbgPrint((DPRINT_FILESYSTEM, "EXT2 Directory Entry:\n"));
323 DbgPrint((DPRINT_FILESYSTEM, "inode = %d\n", DirectoryEntry->inode));
324 DbgPrint((DPRINT_FILESYSTEM, "rec_len = %d\n", DirectoryEntry->rec_len));
325 DbgPrint((DPRINT_FILESYSTEM, "name_len = %d\n", DirectoryEntry->name_len));
326 DbgPrint((DPRINT_FILESYSTEM, "file_type = %d\n", DirectoryEntry->file_type));
327 DbgPrint((DPRINT_FILESYSTEM, "name = "));
328 for (CurrentOffset=0; CurrentOffset<DirectoryEntry->name_len; CurrentOffset++)
329 {
330 DbgPrint((DPRINT_FILESYSTEM, "%c", DirectoryEntry->name[CurrentOffset]));
331 }
332 DbgPrint((DPRINT_FILESYSTEM, "\n"));
333
334 return TRUE;
335 }
336
337 CurrentOffset += CurrentDirectoryEntry->rec_len;
338 }
339
340 return FALSE;
341 }
342
343 /*
344 * Ext2ReadFile()
345 * Reads BytesToRead from open file and
346 * returns the number of bytes read in BytesRead
347 */
348 BOOL Ext2ReadFile(FILE *FileHandle, U64 BytesToRead, U64* BytesRead, PVOID Buffer)
349 {
350 PEXT2_FILE_INFO Ext2FileInfo = (PEXT2_FILE_INFO)FileHandle;
351 U32 BlockNumber;
352 U32 BlockNumberIndex;
353 U32 OffsetInBlock;
354 U32 LengthInBlock;
355 U32 NumberOfBlocks;
356
357 DbgPrint((DPRINT_FILESYSTEM, "Ext2ReadFile() BytesToRead = %d Buffer = 0x%x\n", (U32)BytesToRead, Buffer));
358
359 if (BytesRead != NULL)
360 {
361 *BytesRead = 0;
362 }
363
364 // Make sure we have the block pointer list if we need it
365 if (Ext2FileInfo->FileBlockList == NULL)
366 {
367 // Block pointer list is NULL
368 // so this better be a fast symbolic link or else
369 if (((Ext2FileInfo->Inode.i_mode & EXT2_S_IFMT) != EXT2_S_IFLNK) ||
370 (Ext2FileInfo->FileSize > FAST_SYMLINK_MAX_NAME_SIZE))
371 {
372 FileSystemError("Block pointer list is NULL and file is not a fast symbolic link.");
373 return FALSE;
374 }
375 }
376
377 //
378 // If they are trying to read past the
379 // end of the file then return success
380 // with BytesRead == 0
381 //
382 if (Ext2FileInfo->FilePointer >= Ext2FileInfo->FileSize)
383 {
384 return TRUE;
385 }
386
387 //
388 // If they are trying to read more than there is to read
389 // then adjust the amount to read
390 //
391 if ((Ext2FileInfo->FilePointer + BytesToRead) > Ext2FileInfo->FileSize)
392 {
393 BytesToRead = (Ext2FileInfo->FileSize - Ext2FileInfo->FilePointer);
394 }
395
396 // Check if this is a fast symbolic link
397 // if so then the read is easy
398 if (((Ext2FileInfo->Inode.i_mode & EXT2_S_IFMT) == EXT2_S_IFLNK) &&
399 (Ext2FileInfo->FileSize <= FAST_SYMLINK_MAX_NAME_SIZE))
400 {
401 DbgPrint((DPRINT_FILESYSTEM, "Reading fast symbolic link data\n"));
402
403 // Copy the data from the link
404 RtlCopyMemory(Buffer, (PVOID)(Ext2FileInfo->Inode.i_block) + Ext2FileInfo->FilePointer, BytesToRead);
405
406 if (BytesRead != NULL)
407 {
408 *BytesRead = BytesToRead;
409 }
410
411 return TRUE;
412 }
413
414 //
415 // Ok, now we have to perform at most 3 calculations
416 // I'll draw you a picture (using nifty ASCII art):
417 //
418 // CurrentFilePointer -+
419 // |
420 // +----------------+
421 // |
422 // +-----------+-----------+-----------+-----------+
423 // | Block 1 | Block 2 | Block 3 | Block 4 |
424 // +-----------+-----------+-----------+-----------+
425 // | |
426 // +---------------+--------------------+
427 // |
428 // BytesToRead -------+
429 //
430 // 1 - The first calculation (and read) will align
431 // the file pointer with the next block.
432 // boundary (if we are supposed to read that much)
433 // 2 - The next calculation (and read) will read
434 // in all the full blocks that the requested
435 // amount of data would cover (in this case
436 // blocks 2 & 3).
437 // 3 - The last calculation (and read) would read
438 // in the remainder of the data requested out of
439 // the last block.
440 //
441
442 //
443 // Only do the first read if we
444 // aren't aligned on a block boundary
445 //
446 if (Ext2FileInfo->FilePointer % Ext2BlockSizeInBytes)
447 {
448 //
449 // Do the math for our first read
450 //
451 BlockNumberIndex = (Ext2FileInfo->FilePointer / Ext2BlockSizeInBytes);
452 BlockNumber = Ext2FileInfo->FileBlockList[BlockNumberIndex];
453 OffsetInBlock = (Ext2FileInfo->FilePointer % Ext2BlockSizeInBytes);
454 LengthInBlock = (BytesToRead > (Ext2BlockSizeInBytes - OffsetInBlock)) ? (Ext2BlockSizeInBytes - OffsetInBlock) : BytesToRead;
455
456 //
457 // Now do the read and update BytesRead, BytesToRead, FilePointer, & Buffer
458 //
459 if (!Ext2ReadPartialBlock(BlockNumber, OffsetInBlock, LengthInBlock, Buffer))
460 {
461 return FALSE;
462 }
463 if (BytesRead != NULL)
464 {
465 *BytesRead += LengthInBlock;
466 }
467 BytesToRead -= LengthInBlock;
468 Ext2FileInfo->FilePointer += LengthInBlock;
469 Buffer += LengthInBlock;
470 }
471
472 //
473 // Do the math for our second read (if any data left)
474 //
475 if (BytesToRead > 0)
476 {
477 //
478 // Determine how many full clusters we need to read
479 //
480 NumberOfBlocks = (BytesToRead / Ext2BlockSizeInBytes);
481
482 while (NumberOfBlocks > 0)
483 {
484 BlockNumberIndex = (Ext2FileInfo->FilePointer / Ext2BlockSizeInBytes);
485 BlockNumber = Ext2FileInfo->FileBlockList[BlockNumberIndex];
486
487 //
488 // Now do the read and update BytesRead, BytesToRead, FilePointer, & Buffer
489 //
490 if (!Ext2ReadBlock(BlockNumber, Buffer))
491 {
492 return FALSE;
493 }
494 if (BytesRead != NULL)
495 {
496 *BytesRead += Ext2BlockSizeInBytes;
497 }
498 BytesToRead -= Ext2BlockSizeInBytes;
499 Ext2FileInfo->FilePointer += Ext2BlockSizeInBytes;
500 Buffer += Ext2BlockSizeInBytes;
501 NumberOfBlocks--;
502 }
503 }
504
505 //
506 // Do the math for our third read (if any data left)
507 //
508 if (BytesToRead > 0)
509 {
510 BlockNumberIndex = (Ext2FileInfo->FilePointer / Ext2BlockSizeInBytes);
511 BlockNumber = Ext2FileInfo->FileBlockList[BlockNumberIndex];
512
513 //
514 // Now do the read and update BytesRead, BytesToRead, FilePointer, & Buffer
515 //
516 if (!Ext2ReadPartialBlock(BlockNumber, 0, BytesToRead, Buffer))
517 {
518 return FALSE;
519 }
520 if (BytesRead != NULL)
521 {
522 *BytesRead += BytesToRead;
523 }
524 Ext2FileInfo->FilePointer += BytesToRead;
525 BytesToRead -= BytesToRead;
526 Buffer += BytesToRead;
527 }
528
529 return TRUE;
530 }
531
532 U64 Ext2GetFileSize(FILE *FileHandle)
533 {
534 PEXT2_FILE_INFO Ext2FileHandle = (PEXT2_FILE_INFO)FileHandle;
535
536 DbgPrint((DPRINT_FILESYSTEM, "Ext2GetFileSize() FileSize = %d\n", Ext2FileHandle->FileSize));
537
538 return Ext2FileHandle->FileSize;
539 }
540
541 VOID Ext2SetFilePointer(FILE *FileHandle, U64 NewFilePointer)
542 {
543 PEXT2_FILE_INFO Ext2FileHandle = (PEXT2_FILE_INFO)FileHandle;
544
545 DbgPrint((DPRINT_FILESYSTEM, "Ext2SetFilePointer() NewFilePointer = %d\n", NewFilePointer));
546
547 Ext2FileHandle->FilePointer = NewFilePointer;
548 }
549
550 U64 Ext2GetFilePointer(FILE *FileHandle)
551 {
552 PEXT2_FILE_INFO Ext2FileHandle = (PEXT2_FILE_INFO)FileHandle;
553
554 DbgPrint((DPRINT_FILESYSTEM, "Ext2GetFilePointer() FilePointer = %d\n", Ext2FileHandle->FilePointer));
555
556 return Ext2FileHandle->FilePointer;
557 }
558
559 BOOL Ext2ReadVolumeSectors(U8 DriveNumber, U64 SectorNumber, U64 SectorCount, PVOID Buffer)
560 {
561 //GEOMETRY DiskGeometry;
562 //BOOL ReturnValue;
563 //if (!DiskGetDriveGeometry(DriveNumber, &DiskGeometry))
564 //{
565 // return FALSE;
566 //}
567 //ReturnValue = MachDiskReadLogicalSectors(DriveNumber, SectorNumber + Ext2VolumeStartSector, SectorCount, (PVOID)DISKREADBUFFER);
568 //RtlCopyMemory(Buffer, (PVOID)DISKREADBUFFER, SectorCount * DiskGeometry.BytesPerSector);
569 //return ReturnValue;
570
571 return CacheReadDiskSectors(DriveNumber, SectorNumber + Ext2VolumeStartSector, SectorCount, Buffer);
572 }
573
574 BOOL Ext2ReadSuperBlock(VOID)
575 {
576
577 DbgPrint((DPRINT_FILESYSTEM, "Ext2ReadSuperBlock()\n"));
578
579 //
580 // Free any memory previously allocated
581 //
582 if (Ext2SuperBlock != NULL)
583 {
584 MmFreeMemory(Ext2SuperBlock);
585
586 Ext2SuperBlock = NULL;
587 }
588
589 //
590 // Now allocate the memory to hold the super block
591 //
592 Ext2SuperBlock = (PEXT2_SUPER_BLOCK)MmAllocateMemory(1024);
593
594 //
595 // Make sure we got the memory
596 //
597 if (Ext2SuperBlock == NULL)
598 {
599 FileSystemError("Out of memory.");
600 return FALSE;
601 }
602
603 // Now try to read the super block
604 // If this fails then abort
605 if (!MachDiskReadLogicalSectors(Ext2DriveNumber, Ext2VolumeStartSector, 8, (PVOID)DISKREADBUFFER))
606 {
607 return FALSE;
608 }
609 RtlCopyMemory(Ext2SuperBlock, (PVOID)(DISKREADBUFFER + 1024), 1024);
610
611 DbgPrint((DPRINT_FILESYSTEM, "Dumping super block:\n"));
612
613 DbgPrint((DPRINT_FILESYSTEM, "s_inodes_count: %d\n", Ext2SuperBlock->s_inodes_count));
614 DbgPrint((DPRINT_FILESYSTEM, "s_blocks_count: %d\n", Ext2SuperBlock->s_blocks_count));
615 DbgPrint((DPRINT_FILESYSTEM, "s_r_blocks_count: %d\n", Ext2SuperBlock->s_r_blocks_count));
616 DbgPrint((DPRINT_FILESYSTEM, "s_free_blocks_count: %d\n", Ext2SuperBlock->s_free_blocks_count));
617 DbgPrint((DPRINT_FILESYSTEM, "s_free_inodes_count: %d\n", Ext2SuperBlock->s_free_inodes_count));
618 DbgPrint((DPRINT_FILESYSTEM, "s_first_data_block: %d\n", Ext2SuperBlock->s_first_data_block));
619 DbgPrint((DPRINT_FILESYSTEM, "s_log_block_size: %d\n", Ext2SuperBlock->s_log_block_size));
620 DbgPrint((DPRINT_FILESYSTEM, "s_log_frag_size: %d\n", Ext2SuperBlock->s_log_frag_size));
621 DbgPrint((DPRINT_FILESYSTEM, "s_blocks_per_group: %d\n", Ext2SuperBlock->s_blocks_per_group));
622 DbgPrint((DPRINT_FILESYSTEM, "s_frags_per_group: %d\n", Ext2SuperBlock->s_frags_per_group));
623 DbgPrint((DPRINT_FILESYSTEM, "s_inodes_per_group: %d\n", Ext2SuperBlock->s_inodes_per_group));
624 DbgPrint((DPRINT_FILESYSTEM, "s_mtime: %d\n", Ext2SuperBlock->s_mtime));
625 DbgPrint((DPRINT_FILESYSTEM, "s_wtime: %d\n", Ext2SuperBlock->s_wtime));
626 DbgPrint((DPRINT_FILESYSTEM, "s_mnt_count: %d\n", Ext2SuperBlock->s_mnt_count));
627 DbgPrint((DPRINT_FILESYSTEM, "s_max_mnt_count: %d\n", Ext2SuperBlock->s_max_mnt_count));
628 DbgPrint((DPRINT_FILESYSTEM, "s_magic: 0x%x\n", Ext2SuperBlock->s_magic));
629 DbgPrint((DPRINT_FILESYSTEM, "s_state: %d\n", Ext2SuperBlock->s_state));
630 DbgPrint((DPRINT_FILESYSTEM, "s_errors: %d\n", Ext2SuperBlock->s_errors));
631 DbgPrint((DPRINT_FILESYSTEM, "s_minor_rev_level: %d\n", Ext2SuperBlock->s_minor_rev_level));
632 DbgPrint((DPRINT_FILESYSTEM, "s_lastcheck: %d\n", Ext2SuperBlock->s_lastcheck));
633 DbgPrint((DPRINT_FILESYSTEM, "s_checkinterval: %d\n", Ext2SuperBlock->s_checkinterval));
634 DbgPrint((DPRINT_FILESYSTEM, "s_creator_os: %d\n", Ext2SuperBlock->s_creator_os));
635 DbgPrint((DPRINT_FILESYSTEM, "s_rev_level: %d\n", Ext2SuperBlock->s_rev_level));
636 DbgPrint((DPRINT_FILESYSTEM, "s_def_resuid: %d\n", Ext2SuperBlock->s_def_resuid));
637 DbgPrint((DPRINT_FILESYSTEM, "s_def_resgid: %d\n", Ext2SuperBlock->s_def_resgid));
638 DbgPrint((DPRINT_FILESYSTEM, "s_first_ino: %d\n", Ext2SuperBlock->s_first_ino));
639 DbgPrint((DPRINT_FILESYSTEM, "s_inode_size: %d\n", Ext2SuperBlock->s_inode_size));
640 DbgPrint((DPRINT_FILESYSTEM, "s_block_group_nr: %d\n", Ext2SuperBlock->s_block_group_nr));
641 DbgPrint((DPRINT_FILESYSTEM, "s_feature_compat: 0x%x\n", Ext2SuperBlock->s_feature_compat));
642 DbgPrint((DPRINT_FILESYSTEM, "s_feature_incompat: 0x%x\n", Ext2SuperBlock->s_feature_incompat));
643 DbgPrint((DPRINT_FILESYSTEM, "s_feature_ro_compat: 0x%x\n", Ext2SuperBlock->s_feature_ro_compat));
644 DbgPrint((DPRINT_FILESYSTEM, "s_uuid[16] = 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n", Ext2SuperBlock->s_uuid[0], Ext2SuperBlock->s_uuid[1], Ext2SuperBlock->s_uuid[2], Ext2SuperBlock->s_uuid[3], Ext2SuperBlock->s_uuid[4], Ext2SuperBlock->s_uuid[5], Ext2SuperBlock->s_uuid[6], Ext2SuperBlock->s_uuid[7], Ext2SuperBlock->s_uuid[8], Ext2SuperBlock->s_uuid[9], Ext2SuperBlock->s_uuid[10], Ext2SuperBlock->s_uuid[11], Ext2SuperBlock->s_uuid[12], Ext2SuperBlock->s_uuid[13], Ext2SuperBlock->s_uuid[14], Ext2SuperBlock->s_uuid[15]));
645 DbgPrint((DPRINT_FILESYSTEM, "s_volume_name[16] = '%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c'\n", Ext2SuperBlock->s_volume_name[0], Ext2SuperBlock->s_volume_name[1], Ext2SuperBlock->s_volume_name[2], Ext2SuperBlock->s_volume_name[3], Ext2SuperBlock->s_volume_name[4], Ext2SuperBlock->s_volume_name[5], Ext2SuperBlock->s_volume_name[6], Ext2SuperBlock->s_volume_name[7], Ext2SuperBlock->s_volume_name[8], Ext2SuperBlock->s_volume_name[9], Ext2SuperBlock->s_volume_name[10], Ext2SuperBlock->s_volume_name[11], Ext2SuperBlock->s_volume_name[12], Ext2SuperBlock->s_volume_name[13], Ext2SuperBlock->s_volume_name[14], Ext2SuperBlock->s_volume_name[15]));
646 DbgPrint((DPRINT_FILESYSTEM, "s_last_mounted[64]='%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c'\n", Ext2SuperBlock->s_last_mounted[0], Ext2SuperBlock->s_last_mounted[1], Ext2SuperBlock->s_last_mounted[2], Ext2SuperBlock->s_last_mounted[3], Ext2SuperBlock->s_last_mounted[4], Ext2SuperBlock->s_last_mounted[5], Ext2SuperBlock->s_last_mounted[6], Ext2SuperBlock->s_last_mounted[7], Ext2SuperBlock->s_last_mounted[8], Ext2SuperBlock->s_last_mounted[9],
647 Ext2SuperBlock->s_last_mounted[10], Ext2SuperBlock->s_last_mounted[11], Ext2SuperBlock->s_last_mounted[12], Ext2SuperBlock->s_last_mounted[13], Ext2SuperBlock->s_last_mounted[14], Ext2SuperBlock->s_last_mounted[15], Ext2SuperBlock->s_last_mounted[16], Ext2SuperBlock->s_last_mounted[17], Ext2SuperBlock->s_last_mounted[18], Ext2SuperBlock->s_last_mounted[19],
648 Ext2SuperBlock->s_last_mounted[20], Ext2SuperBlock->s_last_mounted[21], Ext2SuperBlock->s_last_mounted[22], Ext2SuperBlock->s_last_mounted[23], Ext2SuperBlock->s_last_mounted[24], Ext2SuperBlock->s_last_mounted[25], Ext2SuperBlock->s_last_mounted[26], Ext2SuperBlock->s_last_mounted[27], Ext2SuperBlock->s_last_mounted[28], Ext2SuperBlock->s_last_mounted[29],
649 Ext2SuperBlock->s_last_mounted[30], Ext2SuperBlock->s_last_mounted[31], Ext2SuperBlock->s_last_mounted[32], Ext2SuperBlock->s_last_mounted[33], Ext2SuperBlock->s_last_mounted[34], Ext2SuperBlock->s_last_mounted[35], Ext2SuperBlock->s_last_mounted[36], Ext2SuperBlock->s_last_mounted[37], Ext2SuperBlock->s_last_mounted[38], Ext2SuperBlock->s_last_mounted[39],
650 Ext2SuperBlock->s_last_mounted[40], Ext2SuperBlock->s_last_mounted[41], Ext2SuperBlock->s_last_mounted[42], Ext2SuperBlock->s_last_mounted[43], Ext2SuperBlock->s_last_mounted[44], Ext2SuperBlock->s_last_mounted[45], Ext2SuperBlock->s_last_mounted[46], Ext2SuperBlock->s_last_mounted[47], Ext2SuperBlock->s_last_mounted[48], Ext2SuperBlock->s_last_mounted[49],
651 Ext2SuperBlock->s_last_mounted[50], Ext2SuperBlock->s_last_mounted[51], Ext2SuperBlock->s_last_mounted[52], Ext2SuperBlock->s_last_mounted[53], Ext2SuperBlock->s_last_mounted[54], Ext2SuperBlock->s_last_mounted[55], Ext2SuperBlock->s_last_mounted[56], Ext2SuperBlock->s_last_mounted[57], Ext2SuperBlock->s_last_mounted[58], Ext2SuperBlock->s_last_mounted[59],
652 Ext2SuperBlock->s_last_mounted[60], Ext2SuperBlock->s_last_mounted[61], Ext2SuperBlock->s_last_mounted[62], Ext2SuperBlock->s_last_mounted[63]));
653 DbgPrint((DPRINT_FILESYSTEM, "s_algorithm_usage_bitmap = 0x%x\n", Ext2SuperBlock->s_algorithm_usage_bitmap));
654 DbgPrint((DPRINT_FILESYSTEM, "s_prealloc_blocks = %d\n", Ext2SuperBlock->s_prealloc_blocks));
655 DbgPrint((DPRINT_FILESYSTEM, "s_prealloc_dir_blocks = %d\n", Ext2SuperBlock->s_prealloc_dir_blocks));
656 DbgPrint((DPRINT_FILESYSTEM, "s_padding1 = %d\n", Ext2SuperBlock->s_padding1));
657 DbgPrint((DPRINT_FILESYSTEM, "s_journal_uuid[16] = 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n", Ext2SuperBlock->s_journal_uuid[0], Ext2SuperBlock->s_journal_uuid[1], Ext2SuperBlock->s_journal_uuid[2], Ext2SuperBlock->s_journal_uuid[3], Ext2SuperBlock->s_journal_uuid[4], Ext2SuperBlock->s_journal_uuid[5], Ext2SuperBlock->s_journal_uuid[6], Ext2SuperBlock->s_journal_uuid[7], Ext2SuperBlock->s_journal_uuid[8], Ext2SuperBlock->s_journal_uuid[9], Ext2SuperBlock->s_journal_uuid[10], Ext2SuperBlock->s_journal_uuid[11], Ext2SuperBlock->s_journal_uuid[12], Ext2SuperBlock->s_journal_uuid[13], Ext2SuperBlock->s_journal_uuid[14], Ext2SuperBlock->s_journal_uuid[15]));
658 DbgPrint((DPRINT_FILESYSTEM, "s_journal_inum = %d\n", Ext2SuperBlock->s_journal_inum));
659 DbgPrint((DPRINT_FILESYSTEM, "s_journal_dev = %d\n", Ext2SuperBlock->s_journal_dev));
660 DbgPrint((DPRINT_FILESYSTEM, "s_last_orphan = %d\n", Ext2SuperBlock->s_last_orphan));
661
662 //
663 // Check the super block magic
664 //
665 if (Ext2SuperBlock->s_magic != EXT3_SUPER_MAGIC)
666 {
667 FileSystemError("Invalid super block magic (0xef53)");
668 return FALSE;
669 }
670
671 //
672 // Check the revision level
673 //
674 if (Ext2SuperBlock->s_rev_level > EXT3_DYNAMIC_REV)
675 {
676 FileSystemError("FreeLoader does not understand the revision of this EXT2/EXT3 filesystem.\nPlease update FreeLoader.");
677 return FALSE;
678 }
679
680 //
681 // Check the feature set
682 // Don't need to check the compatible or read-only compatible features
683 // because we only mount the filesystem as read-only
684 //
685 if ((Ext2SuperBlock->s_rev_level >= EXT3_DYNAMIC_REV) &&
686 (/*((Ext2SuperBlock->s_feature_compat & ~EXT3_FEATURE_COMPAT_SUPP) != 0) ||*/
687 /*((Ext2SuperBlock->s_feature_ro_compat & ~EXT3_FEATURE_RO_COMPAT_SUPP) != 0) ||*/
688 ((Ext2SuperBlock->s_feature_incompat & ~EXT3_FEATURE_INCOMPAT_SUPP) != 0)))
689 {
690 FileSystemError("FreeLoader does not understand features of this EXT2/EXT3 filesystem.\nPlease update FreeLoader.");
691 return FALSE;
692 }
693
694 // Calculate the group count
695 Ext2GroupCount = (Ext2SuperBlock->s_blocks_count - Ext2SuperBlock->s_first_data_block + Ext2SuperBlock->s_blocks_per_group - 1) / Ext2SuperBlock->s_blocks_per_group;
696 DbgPrint((DPRINT_FILESYSTEM, "Ext2GroupCount: %d\n", Ext2GroupCount));
697
698 // Calculate the block size
699 Ext2BlockSizeInBytes = 1024 << Ext2SuperBlock->s_log_block_size;
700 Ext2BlockSizeInSectors = Ext2BlockSizeInBytes / Ext2DiskGeometry.BytesPerSector;
701 DbgPrint((DPRINT_FILESYSTEM, "Ext2BlockSizeInBytes: %d\n", Ext2BlockSizeInBytes));
702 DbgPrint((DPRINT_FILESYSTEM, "Ext2BlockSizeInSectors: %d\n", Ext2BlockSizeInSectors));
703
704 // Calculate the fragment size
705 if (Ext2SuperBlock->s_log_frag_size >= 0)
706 {
707 Ext2FragmentSizeInBytes = 1024 << Ext2SuperBlock->s_log_frag_size;
708 }
709 else
710 {
711 Ext2FragmentSizeInBytes = 1024 >> -(Ext2SuperBlock->s_log_frag_size);
712 }
713 Ext2FragmentSizeInSectors = Ext2FragmentSizeInBytes / Ext2DiskGeometry.BytesPerSector;
714 DbgPrint((DPRINT_FILESYSTEM, "Ext2FragmentSizeInBytes: %d\n", Ext2FragmentSizeInBytes));
715 DbgPrint((DPRINT_FILESYSTEM, "Ext2FragmentSizeInSectors: %d\n", Ext2FragmentSizeInSectors));
716
717 // Verify that the fragment size and the block size are equal
718 if (Ext2BlockSizeInBytes != Ext2FragmentSizeInBytes)
719 {
720 FileSystemError("The fragment size must be equal to the block size.");
721 return FALSE;
722 }
723
724 // Calculate the number of inodes in one block
725 Ext2InodesPerBlock = Ext2BlockSizeInBytes / EXT3_INODE_SIZE(Ext2SuperBlock);
726 DbgPrint((DPRINT_FILESYSTEM, "Ext2InodesPerBlock: %d\n", Ext2InodesPerBlock));
727
728 // Calculate the number of group descriptors in one block
729 Ext2GroupDescPerBlock = EXT3_DESC_PER_BLOCK(Ext2SuperBlock);
730 DbgPrint((DPRINT_FILESYSTEM, "Ext2GroupDescPerBlock: %d\n", Ext2GroupDescPerBlock));
731
732 return TRUE;
733 }
734
735 BOOL Ext2ReadGroupDescriptors(VOID)
736 {
737 U32 GroupDescBlockCount;
738 U32 CurrentGroupDescBlock;
739
740 DbgPrint((DPRINT_FILESYSTEM, "Ext2ReadGroupDescriptors()\n"));
741
742 //
743 // Free any memory previously allocated
744 //
745 if (Ext2GroupDescriptors != NULL)
746 {
747 MmFreeMemory(Ext2GroupDescriptors);
748
749 Ext2GroupDescriptors = NULL;
750 }
751
752 //
753 // Now allocate the memory to hold the group descriptors
754 //
755 GroupDescBlockCount = ROUND_UP(Ext2GroupCount, Ext2GroupDescPerBlock) / Ext2GroupDescPerBlock;
756 Ext2GroupDescriptors = (PEXT2_GROUP_DESC)MmAllocateMemory(GroupDescBlockCount * Ext2BlockSizeInBytes);
757
758 //
759 // Make sure we got the memory
760 //
761 if (Ext2GroupDescriptors == NULL)
762 {
763 FileSystemError("Out of memory.");
764 return FALSE;
765 }
766
767 // Now read the group descriptors
768 for (CurrentGroupDescBlock=0; CurrentGroupDescBlock<GroupDescBlockCount; CurrentGroupDescBlock++)
769 {
770 if (!Ext2ReadBlock(Ext2SuperBlock->s_first_data_block + 1 + CurrentGroupDescBlock, (PVOID)FILESYSBUFFER))
771 {
772 return FALSE;
773 }
774
775 RtlCopyMemory((Ext2GroupDescriptors + (CurrentGroupDescBlock * Ext2BlockSizeInBytes)), (PVOID)FILESYSBUFFER, Ext2BlockSizeInBytes);
776 }
777
778 return TRUE;
779 }
780
781 BOOL Ext2ReadDirectory(U32 Inode, PVOID* DirectoryBuffer, PEXT2_INODE InodePointer)
782 {
783 EXT2_FILE_INFO DirectoryFileInfo;
784
785 DbgPrint((DPRINT_FILESYSTEM, "Ext2ReadDirectory() Inode = %d\n", Inode));
786
787 // Read the directory inode
788 if (!Ext2ReadInode(Inode, InodePointer))
789 {
790 return FALSE;
791 }
792
793 // Make sure it is a directory inode
794 if ((InodePointer->i_mode & EXT2_S_IFMT) != EXT2_S_IFDIR)
795 {
796 FileSystemError("Inode is not a directory.");
797 return FALSE;
798 }
799
800 // Fill in file info struct so we can call Ext2ReadFile()
801 RtlZeroMemory(&DirectoryFileInfo, sizeof(EXT2_FILE_INFO));
802 DirectoryFileInfo.DriveNumber = Ext2DriveNumber;
803 DirectoryFileInfo.FileBlockList = Ext2ReadBlockPointerList(InodePointer);
804 DirectoryFileInfo.FilePointer = 0;
805 DirectoryFileInfo.FileSize = Ext2GetInodeFileSize(InodePointer);
806
807 if (DirectoryFileInfo.FileBlockList == NULL)
808 {
809 return FALSE;
810 }
811
812 //
813 // Now allocate the memory to hold the group descriptors
814 //
815 *DirectoryBuffer = (PEXT2_DIR_ENTRY)MmAllocateMemory(DirectoryFileInfo.FileSize);
816
817 //
818 // Make sure we got the memory
819 //
820 if (*DirectoryBuffer == NULL)
821 {
822 MmFreeMemory(DirectoryFileInfo.FileBlockList);
823 FileSystemError("Out of memory.");
824 return FALSE;
825 }
826
827 // Now read the root directory data
828 if (!Ext2ReadFile(&DirectoryFileInfo, DirectoryFileInfo.FileSize, NULL, *DirectoryBuffer))
829 {
830 MmFreeMemory(*DirectoryBuffer);
831 *DirectoryBuffer = NULL;
832 MmFreeMemory(DirectoryFileInfo.FileBlockList);
833 return FALSE;
834 }
835
836 MmFreeMemory(DirectoryFileInfo.FileBlockList);
837 return TRUE;
838 }
839
840 BOOL Ext2ReadBlock(U32 BlockNumber, PVOID Buffer)
841 {
842 UCHAR ErrorString[80];
843
844 DbgPrint((DPRINT_FILESYSTEM, "Ext2ReadBlock() BlockNumber = %d Buffer = 0x%x\n", BlockNumber, Buffer));
845
846 // Make sure its a valid block
847 if (BlockNumber > Ext2SuperBlock->s_blocks_count)
848 {
849 sprintf(ErrorString, "Error reading block %d - block out of range.", (int) BlockNumber);
850 FileSystemError(ErrorString);
851 return FALSE;
852 }
853
854 // Check to see if this is a sparse block
855 if (BlockNumber == 0)
856 {
857 DbgPrint((DPRINT_FILESYSTEM, "Block is part of a sparse file. Zeroing input buffer.\n"));
858
859 RtlZeroMemory(Buffer, Ext2BlockSizeInBytes);
860
861 return TRUE;
862 }
863
864 return Ext2ReadVolumeSectors(Ext2DriveNumber, (U64)BlockNumber * Ext2BlockSizeInSectors, Ext2BlockSizeInSectors, Buffer);
865 }
866
867 /*
868 * Ext2ReadPartialBlock()
869 * Reads part of a block into memory
870 */
871 BOOL Ext2ReadPartialBlock(U32 BlockNumber, U32 StartingOffset, U32 Length, PVOID Buffer)
872 {
873
874 DbgPrint((DPRINT_FILESYSTEM, "Ext2ReadPartialBlock() BlockNumber = %d StartingOffset = %d Length = %d Buffer = 0x%x\n", BlockNumber, StartingOffset, Length, Buffer));
875
876 if (!Ext2ReadBlock(BlockNumber, (PVOID)FILESYSBUFFER))
877 {
878 return FALSE;
879 }
880
881 memcpy(Buffer, ((PVOID)FILESYSBUFFER + StartingOffset), Length);
882
883 return TRUE;
884 }
885
886 U32 Ext2GetGroupDescBlockNumber(U32 Group)
887 {
888 return (((Group * sizeof(EXT2_GROUP_DESC)) / Ext2GroupDescPerBlock) + Ext2SuperBlock->s_first_data_block + 1);
889 }
890
891 U32 Ext2GetGroupDescOffsetInBlock(U32 Group)
892 {
893 return ((Group * sizeof(EXT2_GROUP_DESC)) % Ext2GroupDescPerBlock);
894 }
895
896 U32 Ext2GetInodeGroupNumber(U32 Inode)
897 {
898 return ((Inode - 1) / Ext2SuperBlock->s_inodes_per_group);
899 }
900
901 U32 Ext2GetInodeBlockNumber(U32 Inode)
902 {
903 return (((Inode - 1) % Ext2SuperBlock->s_inodes_per_group) / Ext2InodesPerBlock);
904 }
905
906 U32 Ext2GetInodeOffsetInBlock(U32 Inode)
907 {
908 return (((Inode - 1) % Ext2SuperBlock->s_inodes_per_group) % Ext2InodesPerBlock);
909 }
910
911 BOOL Ext2ReadInode(U32 Inode, PEXT2_INODE InodeBuffer)
912 {
913 U32 InodeGroupNumber;
914 U32 InodeBlockNumber;
915 U32 InodeOffsetInBlock;
916 UCHAR ErrorString[80];
917 EXT2_GROUP_DESC GroupDescriptor;
918
919 DbgPrint((DPRINT_FILESYSTEM, "Ext2ReadInode() Inode = %d\n", Inode));
920
921 // Make sure its a valid inode
922 if ((Inode < 1) || (Inode > Ext2SuperBlock->s_inodes_count))
923 {
924 sprintf(ErrorString, "Error reading inode %ld - inode out of range.", Inode);
925 FileSystemError(ErrorString);
926 return FALSE;
927 }
928
929 // Get inode group & block number and offset in block
930 InodeGroupNumber = Ext2GetInodeGroupNumber(Inode);
931 InodeBlockNumber = Ext2GetInodeBlockNumber(Inode);
932 InodeOffsetInBlock = Ext2GetInodeOffsetInBlock(Inode);
933 DbgPrint((DPRINT_FILESYSTEM, "InodeGroupNumber = %d\n", InodeGroupNumber));
934 DbgPrint((DPRINT_FILESYSTEM, "InodeBlockNumber = %d\n", InodeBlockNumber));
935 DbgPrint((DPRINT_FILESYSTEM, "InodeOffsetInBlock = %d\n", InodeOffsetInBlock));
936
937 // Read the group descriptor
938 if (!Ext2ReadGroupDescriptor(InodeGroupNumber, &GroupDescriptor))
939 {
940 return FALSE;
941 }
942
943 // Add the start block of the inode table to the inode block number
944 InodeBlockNumber += GroupDescriptor.bg_inode_table;
945 DbgPrint((DPRINT_FILESYSTEM, "InodeBlockNumber (after group desc correction) = %d\n", InodeBlockNumber));
946
947 // Read the block
948 if (!Ext2ReadBlock(InodeBlockNumber, (PVOID)FILESYSBUFFER))
949 {
950 return FALSE;
951 }
952
953 // Copy the data to their buffer
954 RtlCopyMemory(InodeBuffer, (PVOID)(FILESYSBUFFER + (InodeOffsetInBlock * EXT3_INODE_SIZE(Ext2SuperBlock))), sizeof(EXT2_INODE));
955
956 DbgPrint((DPRINT_FILESYSTEM, "Dumping inode information:\n"));
957 DbgPrint((DPRINT_FILESYSTEM, "i_mode = 0x%x\n", InodeBuffer->i_mode));
958 DbgPrint((DPRINT_FILESYSTEM, "i_uid = %d\n", InodeBuffer->i_uid));
959 DbgPrint((DPRINT_FILESYSTEM, "i_size = %d\n", InodeBuffer->i_size));
960 DbgPrint((DPRINT_FILESYSTEM, "i_atime = %d\n", InodeBuffer->i_atime));
961 DbgPrint((DPRINT_FILESYSTEM, "i_ctime = %d\n", InodeBuffer->i_ctime));
962 DbgPrint((DPRINT_FILESYSTEM, "i_mtime = %d\n", InodeBuffer->i_mtime));
963 DbgPrint((DPRINT_FILESYSTEM, "i_dtime = %d\n", InodeBuffer->i_dtime));
964 DbgPrint((DPRINT_FILESYSTEM, "i_gid = %d\n", InodeBuffer->i_gid));
965 DbgPrint((DPRINT_FILESYSTEM, "i_links_count = %d\n", InodeBuffer->i_links_count));
966 DbgPrint((DPRINT_FILESYSTEM, "i_blocks = %d\n", InodeBuffer->i_blocks));
967 DbgPrint((DPRINT_FILESYSTEM, "i_flags = 0x%x\n", InodeBuffer->i_flags));
968 DbgPrint((DPRINT_FILESYSTEM, "i_block[EXT3_N_BLOCKS (%d)] =\n%d,\n%d,\n%d,\n%d,\n%d,\n%d,\n%d,\n%d,\n%d,\n%d,\n%d,\n%d,\n%d,\n%d,\n%d\n", EXT3_N_BLOCKS, InodeBuffer->i_block[0], InodeBuffer->i_block[1], InodeBuffer->i_block[2], InodeBuffer->i_block[3], InodeBuffer->i_block[4], InodeBuffer->i_block[5], InodeBuffer->i_block[6], InodeBuffer->i_block[7], InodeBuffer->i_block[8], InodeBuffer->i_block[9], InodeBuffer->i_block[10], InodeBuffer->i_block[11], InodeBuffer->i_block[12], InodeBuffer->i_block[13], InodeBuffer->i_block[14]));
969 DbgPrint((DPRINT_FILESYSTEM, "i_generation = %d\n", InodeBuffer->i_generation));
970 DbgPrint((DPRINT_FILESYSTEM, "i_file_acl = %d\n", InodeBuffer->i_file_acl));
971 DbgPrint((DPRINT_FILESYSTEM, "i_dir_acl = %d\n", InodeBuffer->i_dir_acl));
972 DbgPrint((DPRINT_FILESYSTEM, "i_faddr = %d\n", InodeBuffer->i_faddr));
973 DbgPrint((DPRINT_FILESYSTEM, "l_i_frag = %d\n", InodeBuffer->osd2.linux2.l_i_frag));
974 DbgPrint((DPRINT_FILESYSTEM, "l_i_fsize = %d\n", InodeBuffer->osd2.linux2.l_i_fsize));
975 DbgPrint((DPRINT_FILESYSTEM, "l_i_uid_high = %d\n", InodeBuffer->osd2.linux2.l_i_uid_high));
976 DbgPrint((DPRINT_FILESYSTEM, "l_i_gid_high = %d\n", InodeBuffer->osd2.linux2.l_i_gid_high));
977
978 return TRUE;
979 }
980
981 BOOL Ext2ReadGroupDescriptor(U32 Group, PEXT2_GROUP_DESC GroupBuffer)
982 {
983 DbgPrint((DPRINT_FILESYSTEM, "Ext2ReadGroupDescriptor()\n"));
984
985 /*if (!Ext2ReadBlock(Ext2GetGroupDescBlockNumber(Group), (PVOID)FILESYSBUFFER))
986 {
987 return FALSE;
988 }
989
990 RtlCopyMemory(GroupBuffer, (PVOID)(FILESYSBUFFER + Ext2GetGroupDescOffsetInBlock(Group)), sizeof(EXT2_GROUP_DESC));*/
991
992 RtlCopyMemory(GroupBuffer, &Ext2GroupDescriptors[Group], sizeof(EXT2_GROUP_DESC));
993
994 DbgPrint((DPRINT_FILESYSTEM, "Dumping group descriptor:\n"));
995 DbgPrint((DPRINT_FILESYSTEM, "bg_block_bitmap = %d\n", GroupBuffer->bg_block_bitmap));
996 DbgPrint((DPRINT_FILESYSTEM, "bg_inode_bitmap = %d\n", GroupBuffer->bg_inode_bitmap));
997 DbgPrint((DPRINT_FILESYSTEM, "bg_inode_table = %d\n", GroupBuffer->bg_inode_table));
998 DbgPrint((DPRINT_FILESYSTEM, "bg_free_blocks_count = %d\n", GroupBuffer->bg_free_blocks_count));
999 DbgPrint((DPRINT_FILESYSTEM, "bg_free_inodes_count = %d\n", GroupBuffer->bg_free_inodes_count));
1000 DbgPrint((DPRINT_FILESYSTEM, "bg_used_dirs_count = %d\n", GroupBuffer->bg_used_dirs_count));
1001
1002 return TRUE;
1003 }
1004
1005 U32* Ext2ReadBlockPointerList(PEXT2_INODE Inode)
1006 {
1007 U64 FileSize;
1008 U32 BlockCount;
1009 U32* BlockList;
1010 U32 CurrentBlockInList;
1011 U32 CurrentBlock;
1012
1013 DbgPrint((DPRINT_FILESYSTEM, "Ext2ReadBlockPointerList()\n"));
1014
1015 // Get the number of blocks this file occupies
1016 // I would just use Inode->i_blocks but it
1017 // doesn't seem to be the number of blocks
1018 // the file size corresponds to, but instead
1019 // it is much bigger.
1020 //BlockCount = Inode->i_blocks;
1021 FileSize = Ext2GetInodeFileSize(Inode);
1022 FileSize = ROUND_UP(FileSize, Ext2BlockSizeInBytes);
1023 BlockCount = (FileSize / Ext2BlockSizeInBytes);
1024
1025 // Allocate the memory for the block list
1026 BlockList = MmAllocateMemory(BlockCount * sizeof(U32));
1027 if (BlockList == NULL)
1028 {
1029 return NULL;
1030 }
1031
1032 RtlZeroMemory(BlockList, BlockCount * sizeof(U32));
1033 CurrentBlockInList = 0;
1034
1035 // Copy the direct block pointers
1036 for (CurrentBlock=0; CurrentBlockInList<BlockCount && CurrentBlock<EXT3_NDIR_BLOCKS; CurrentBlock++)
1037 {
1038 BlockList[CurrentBlockInList] = Inode->i_block[CurrentBlock];
1039 CurrentBlockInList++;
1040 }
1041
1042 // Copy the indirect block pointers
1043 if (CurrentBlockInList < BlockCount)
1044 {
1045 if (!Ext2CopyIndirectBlockPointers(BlockList, &CurrentBlockInList, BlockCount, Inode->i_block[EXT3_IND_BLOCK]))
1046 {
1047 MmFreeMemory(BlockList);
1048 return FALSE;
1049 }
1050 }
1051
1052 // Copy the double indirect block pointers
1053 if (CurrentBlockInList < BlockCount)
1054 {
1055 if (!Ext2CopyDoubleIndirectBlockPointers(BlockList, &CurrentBlockInList, BlockCount, Inode->i_block[EXT3_DIND_BLOCK]))
1056 {
1057 MmFreeMemory(BlockList);
1058 return FALSE;
1059 }
1060 }
1061
1062 // Copy the triple indirect block pointers
1063 if (CurrentBlockInList < BlockCount)
1064 {
1065 if (!Ext2CopyTripleIndirectBlockPointers(BlockList, &CurrentBlockInList, BlockCount, Inode->i_block[EXT3_TIND_BLOCK]))
1066 {
1067 MmFreeMemory(BlockList);
1068 return FALSE;
1069 }
1070 }
1071
1072 return BlockList;
1073 }
1074
1075 U64 Ext2GetInodeFileSize(PEXT2_INODE Inode)
1076 {
1077 if ((Inode->i_mode & EXT2_S_IFMT) == EXT2_S_IFDIR)
1078 {
1079 return (U64)(Inode->i_size);
1080 }
1081 else
1082 {
1083 return ((U64)(Inode->i_size) | ((U64)(Inode->i_dir_acl) << 32));
1084 }
1085 }
1086
1087 BOOL Ext2CopyIndirectBlockPointers(U32* BlockList, U32* CurrentBlockInList, U32 BlockCount, U32 IndirectBlock)
1088 {
1089 U32* BlockBuffer = (U32*)FILESYSBUFFER;
1090 U32 CurrentBlock;
1091 U32 BlockPointersPerBlock;
1092
1093 DbgPrint((DPRINT_FILESYSTEM, "Ext2CopyIndirectBlockPointers() BlockCount = %d\n", BlockCount));
1094
1095 BlockPointersPerBlock = Ext2BlockSizeInBytes / sizeof(U32);
1096
1097 if (!Ext2ReadBlock(IndirectBlock, BlockBuffer))
1098 {
1099 return FALSE;
1100 }
1101
1102 for (CurrentBlock=0; (*CurrentBlockInList)<BlockCount && CurrentBlock<BlockPointersPerBlock; CurrentBlock++)
1103 {
1104 BlockList[(*CurrentBlockInList)] = BlockBuffer[CurrentBlock];
1105 (*CurrentBlockInList)++;
1106 }
1107
1108 return TRUE;
1109 }
1110
1111 BOOL Ext2CopyDoubleIndirectBlockPointers(U32* BlockList, U32* CurrentBlockInList, U32 BlockCount, U32 DoubleIndirectBlock)
1112 {
1113 U32* BlockBuffer;
1114 U32 CurrentBlock;
1115 U32 BlockPointersPerBlock;
1116
1117 DbgPrint((DPRINT_FILESYSTEM, "Ext2CopyDoubleIndirectBlockPointers() BlockCount = %d\n", BlockCount));
1118
1119 BlockPointersPerBlock = Ext2BlockSizeInBytes / sizeof(U32);
1120
1121 BlockBuffer = (U32*)MmAllocateMemory(Ext2BlockSizeInBytes);
1122 if (BlockBuffer == NULL)
1123 {
1124 return FALSE;
1125 }
1126
1127 if (!Ext2ReadBlock(DoubleIndirectBlock, BlockBuffer))
1128 {
1129 MmFreeMemory(BlockBuffer);
1130 return FALSE;
1131 }
1132
1133 for (CurrentBlock=0; (*CurrentBlockInList)<BlockCount && CurrentBlock<BlockPointersPerBlock; CurrentBlock++)
1134 {
1135 if (!Ext2CopyIndirectBlockPointers(BlockList, CurrentBlockInList, BlockCount, BlockBuffer[CurrentBlock]))
1136 {
1137 MmFreeMemory(BlockBuffer);
1138 return FALSE;
1139 }
1140 }
1141
1142 MmFreeMemory(BlockBuffer);
1143 return TRUE;
1144 }
1145
1146 BOOL Ext2CopyTripleIndirectBlockPointers(U32* BlockList, U32* CurrentBlockInList, U32 BlockCount, U32 TripleIndirectBlock)
1147 {
1148 U32* BlockBuffer;
1149 U32 CurrentBlock;
1150 U32 BlockPointersPerBlock;
1151
1152 DbgPrint((DPRINT_FILESYSTEM, "Ext2CopyTripleIndirectBlockPointers() BlockCount = %d\n", BlockCount));
1153
1154 BlockPointersPerBlock = Ext2BlockSizeInBytes / sizeof(U32);
1155
1156 BlockBuffer = (U32*)MmAllocateMemory(Ext2BlockSizeInBytes);
1157 if (BlockBuffer == NULL)
1158 {
1159 return FALSE;
1160 }
1161
1162 if (!Ext2ReadBlock(TripleIndirectBlock, BlockBuffer))
1163 {
1164 MmFreeMemory(BlockBuffer);
1165 return FALSE;
1166 }
1167
1168 for (CurrentBlock=0; (*CurrentBlockInList)<BlockCount && CurrentBlock<BlockPointersPerBlock; CurrentBlock++)
1169 {
1170 if (!Ext2CopyDoubleIndirectBlockPointers(BlockList, CurrentBlockInList, BlockCount, BlockBuffer[CurrentBlock]))
1171 {
1172 MmFreeMemory(BlockBuffer);
1173 return FALSE;
1174 }
1175 }
1176
1177 MmFreeMemory(BlockBuffer);
1178 return TRUE;
1179 }