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

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