Sorting source tree a bit
[tpg/acess2.git] / KernelLand / Modules / Filesystems / NTFS / main.c
1 /*
2  * Acess2 - NTFS Driver
3  * By John Hodge (thePowersGang)
4  *
5  * main.c - Driver core
6  */
7 #define DEBUG   1
8 #define VERBOSE 0
9 #include <acess.h>
10 #include <vfs.h>
11 #include "common.h"
12 #include <modules.h>
13
14 // === IMPORTS ===
15 extern char     *NTFS_ReadDir(tVFS_Node *Node, int Pos);
16 extern tVFS_Node        *NTFS_FindDir(tVFS_Node *Node, const char *Name);
17
18 // === PROTOTYPES ===
19  int    NTFS_Install(char **Arguments);
20 tVFS_Node       *NTFS_InitDevice(const char *Devices, const char **Options);
21 void    NTFS_Unmount(tVFS_Node *Node);
22 void    NTFS_DumpEntry(tNTFS_Disk *Disk, Uint32 Entry);
23
24 // === GLOBALS ===
25 MODULE_DEFINE(0, 0x0A /*v0.1*/, FS_NTFS, NTFS_Install, NULL);
26 tVFS_Driver     gNTFS_FSInfo = {"ntfs", 0, NTFS_InitDevice, NTFS_Unmount, NULL};
27 tVFS_NodeType   gNTFS_DirType = {
28         .TypeName = "NTFS-File",
29         .ReadDir = NTFS_ReadDir,
30         .FindDir = NTFS_FindDir,
31         .Close = NULL
32         };
33
34 tNTFS_Disk      gNTFS_Disks;
35
36 // === CODE ===
37 /**
38  * \brief Installs the NTFS driver
39  */
40 int NTFS_Install(char **Arguments)
41 {
42         VFS_AddDriver( &gNTFS_FSInfo );
43         return 0;
44 }
45
46 /**
47  * \brief Mount a NTFS volume
48  */
49 tVFS_Node *NTFS_InitDevice(const char *Device, const char **Options)
50 {
51         tNTFS_Disk      *disk;
52         tNTFS_BootSector        bs;
53         
54         disk = malloc( sizeof(tNTFS_Disk) );
55         
56         disk->FD = VFS_Open(Device, VFS_OPENFLAG_READ);
57         if(!disk->FD) {
58                 free(disk);
59                 return NULL;
60         }
61         
62         Log_Debug("FS_NTFS", "&bs = %p", &bs);
63         VFS_ReadAt(disk->FD, 0, 512, &bs);
64         
65         Log_Debug("FS_NTFS", "Jump = %02x%02x%02x",
66                 bs.Jump[0],
67                 bs.Jump[1],
68                 bs.Jump[2]);
69         Log_Debug("FS_NTFS", "SystemID = %02x%02x%02x%02x%02x%02x%02x%02x (%8C)",
70                 bs.SystemID[0], bs.SystemID[1], bs.SystemID[2], bs.SystemID[3],
71                 bs.SystemID[4], bs.SystemID[5], bs.SystemID[6], bs.SystemID[7],
72                 bs.SystemID
73                 );
74         Log_Debug("FS_NTFS", "BytesPerSector = %i", bs.BytesPerSector);
75         Log_Debug("FS_NTFS", "SectorsPerCluster = %i", bs.SectorsPerCluster);
76         Log_Debug("FS_NTFS", "MediaDescriptor = 0x%x", bs.MediaDescriptor);
77         Log_Debug("FS_NTFS", "SectorsPerTrack = %i", bs.SectorsPerTrack);
78         Log_Debug("FS_NTFS", "Heads = %i", bs.Heads);
79         Log_Debug("FS_NTFS", "TotalSectorCount = 0x%llx", bs.TotalSectorCount);
80         Log_Debug("FS_NTFS", "MFTStart = 0x%llx", bs.MFTStart);
81         Log_Debug("FS_NTFS", "MFTMirrorStart = 0x%llx", bs.MFTMirrorStart);
82         Log_Debug("FS_NTFS", "ClustersPerMFTRecord = %i", bs.ClustersPerMFTRecord);
83         Log_Debug("FS_NTFS", "ClustersPerIndexRecord = %i", bs.ClustersPerIndexRecord);
84         Log_Debug("FS_NTFS", "SerialNumber = 0x%llx", bs.SerialNumber);
85         
86         disk->ClusterSize = bs.BytesPerSector * bs.SectorsPerCluster;
87         Log_Debug("NTFS", "Cluster Size = %i KiB", disk->ClusterSize/1024);
88         disk->MFTBase = bs.MFTStart;
89         Log_Debug("NTFS", "MFT Base = %i", disk->MFTBase);
90         Log_Debug("NTFS", "TotalSectorCount = 0x%x", bs.TotalSectorCount);
91         
92         if( bs.ClustersPerMFTRecord < 0 ) {
93                 disk->MFTRecSize = 1 << (-bs.ClustersPerMFTRecord);
94         }
95         else {
96                 disk->MFTRecSize = bs.ClustersPerMFTRecord * disk->ClusterSize;
97         }
98         
99         disk->RootNode.Inode = 5;       // MFT Ent #5 is filesystem root
100         disk->RootNode.ImplPtr = disk;
101         
102         disk->RootNode.UID = 0;
103         disk->RootNode.GID = 0;
104         
105         disk->RootNode.NumACLs = 1;
106         disk->RootNode.ACLs = &gVFS_ACL_EveryoneRX;
107         
108         disk->RootNode.Type = &gNTFS_DirType;
109
110         
111         NTFS_DumpEntry(disk, 5);
112         
113         return &disk->RootNode;
114 }
115
116 /**
117  * \brief Unmount an NTFS Disk
118  */
119 void NTFS_Unmount(tVFS_Node *Node)
120 {
121         
122 }
123
124 /**
125  * \brief Dumps a MFT Entry
126  */
127 void NTFS_DumpEntry(tNTFS_Disk *Disk, Uint32 Entry)
128 {
129         void    *buf = malloc( Disk->MFTRecSize );
130         tNTFS_FILE_Header       *hdr = buf;
131         tNTFS_FILE_Attrib       *attr;
132          int    i;
133         
134         if(!buf) {
135                 Log_Warning("FS_NTFS", "malloc() fail!");
136                 return ;
137         }
138         
139         VFS_ReadAt( Disk->FD,
140                 Disk->MFTBase * Disk->ClusterSize + Entry * Disk->MFTRecSize,
141                 Disk->MFTRecSize,
142                 buf);
143         
144         Log_Debug("FS_NTFS", "MFT Entry #%i", Entry);
145         Log_Debug("FS_NTFS", "- Magic = 0x%08x (%4C)", hdr->Magic, &hdr->Magic);
146         Log_Debug("FS_NTFS", "- UpdateSequenceOfs = 0x%x", hdr->UpdateSequenceOfs);
147         Log_Debug("FS_NTFS", "- UpdateSequenceSize = 0x%x", hdr->UpdateSequenceSize);
148         Log_Debug("FS_NTFS", "- LSN = 0x%x", hdr->LSN);
149         Log_Debug("FS_NTFS", "- SequenceNumber = %i", hdr->SequenceNumber);
150         Log_Debug("FS_NTFS", "- HardLinkCount = %i", hdr->HardLinkCount);
151         Log_Debug("FS_NTFS", "- FirstAttribOfs = 0x%x", hdr->FirstAttribOfs);
152         Log_Debug("FS_NTFS", "- Flags = 0x%x", hdr->Flags);
153         Log_Debug("FS_NTFS", "- RecordSize = 0x%x", hdr->RecordSize);
154         Log_Debug("FS_NTFS", "- RecordSpace = 0x%x", hdr->RecordSpace);
155         Log_Debug("FS_NTFS", "- Reference = 0x%llx", hdr->Reference);
156         Log_Debug("FS_NTFS", "- NextAttribID = 0x%04x", hdr->NextAttribID);
157         
158         attr = (void*)( (char*)hdr + hdr->FirstAttribOfs );
159         i = 0;
160         while( (tVAddr)attr < (tVAddr)hdr + hdr->RecordSize )
161         {
162                 if(attr->Type == 0xFFFFFFFF)    break;
163                 Log_Debug("FS_NTFS", "- Attribute %i", i ++);
164                 Log_Debug("FS_NTFS", " > Type = 0x%x", attr->Type);
165                 Log_Debug("FS_NTFS", " > Size = 0x%x", attr->Size);
166                 Log_Debug("FS_NTFS", " > ResidentFlag = 0x%x", attr->ResidentFlag);
167                 Log_Debug("FS_NTFS", " > NameLength = %i", attr->NameLength);
168                 Log_Debug("FS_NTFS", " > NameOffset = 0x%x", attr->NameOffset);
169                 Log_Debug("FS_NTFS", " > Flags = 0x%x", attr->Flags);
170                 Log_Debug("FS_NTFS", " > AttributeID = 0x%x", attr->AttributeID);
171                 if( !attr->ResidentFlag ) {
172                         Log_Debug("FS_NTFS", " > AttribLen = 0x%x", attr->Resident.AttribLen);
173                         Log_Debug("FS_NTFS", " > AttribOfs = 0x%x", attr->Resident.AttribOfs);
174                         Log_Debug("FS_NTFS", " > IndexedFlag = 0x%x", attr->Resident.IndexedFlag);
175                         Log_Debug("FS_NTFS", " > Name = '%*C'", attr->NameLength, attr->Resident.Name);
176                         Debug_HexDump("FS_NTFS",
177                                 (void*)( (tVAddr)attr + attr->Resident.AttribOfs ),
178                                 attr->Resident.AttribLen
179                                 );
180                 }
181                 else {
182                         Log_Debug("FS_NTFS", " > StartingVCN = 0x%llx", attr->NonResident.StartingVCN);
183                         Log_Debug("FS_NTFS", " > LastVCN = 0x%llx", attr->NonResident.LastVCN);
184                         Log_Debug("FS_NTFS", " > DataRunOfs = 0x%x", attr->NonResident.DataRunOfs);
185                         Log_Debug("FS_NTFS", " > CompressionUnitSize = 0x%x", attr->NonResident.CompressionUnitSize);
186                         Log_Debug("FS_NTFS", " > AllocatedSize = 0x%llx", attr->NonResident.AllocatedSize);
187                         Log_Debug("FS_NTFS", " > RealSize = 0x%llx", attr->NonResident.RealSize);
188                         Log_Debug("FS_NTFS", " > InitiatedSize = 0x%llx", attr->NonResident.InitiatedSize);
189                         Log_Debug("FS_NTFS", " > Name = '%*C'", attr->NameLength, attr->NonResident.Name);
190                 }
191                 
192                 attr = (void*)( (tVAddr)attr + attr->Size );
193         }
194         
195         free(buf);
196 }

UCC git Repository :: git.ucc.asn.au