3 * By John Hodge (thePowersGang)
8 * Reference: ntfsdoc.pdf
19 int NTFS_Install(char **Arguments);
20 int NTFS_Detect(int FD);
21 tVFS_Node *NTFS_InitDevice(const char *Devices, const char **Options);
22 void NTFS_Unmount(tVFS_Node *Node);
23 // - MFT Related Functions
24 tNTFS_FILE_Header *NTFS_GetMFT(tNTFS_Disk *Disk, Uint32 MFTEntry);
25 void NTFS_ReleaseMFT(tNTFS_Disk *Disk, Uint32 MFTEntry, tNTFS_FILE_Header *Entry);
26 tNTFS_Attrib *NTFS_GetAttrib(tNTFS_Disk *Disk, Uint32 MFTEntry, int Type, const char *Name, int DesIdx);
27 size_t NTFS_ReadAttribData(tNTFS_Attrib *Attrib, Uint64 Offset, size_t Length, void *Buffer);
28 void NTFS_DumpEntry(tNTFS_Disk *Disk, Uint32 Entry);
31 MODULE_DEFINE(0, 0x0A /*v0.1*/, FS_NTFS, NTFS_Install, NULL);
32 tVFS_Driver gNTFS_FSInfo = {
34 .Detect = NTFS_Detect,
35 .InitDevice = NTFS_InitDevice,
36 .Unmount = NTFS_Unmount,
37 .GetNodeFromINode = NULL
39 tVFS_NodeType gNTFS_DirType = {
40 .TypeName = "NTFS-Dir",
41 .ReadDir = NTFS_ReadDir,
42 .FindDir = NTFS_FindDir,
45 tVFS_NodeType gNTFS_FileType = {
46 .TypeName = "NTFS-File",
50 tNTFS_Disk gNTFS_Disks;
54 * \brief Installs the NTFS driver
56 int NTFS_Install(char **Arguments)
58 VFS_AddDriver( &gNTFS_FSInfo );
63 * \brief Detect if a volume is NTFS
65 int NTFS_Detect(int FD)
68 VFS_ReadAt(FD, 0, 512, &bs);
70 if( bs.BytesPerSector == 0 || (bs.BytesPerSector & 511) )
73 Uint64 ncluster = bs.TotalSectorCount / bs.SectorsPerCluster;
74 if( bs.MFTStart >= ncluster || bs.MFTMirrorStart >= ncluster )
77 if( memcmp(bs.SystemID, "NTFS ", 8) != 0 )
84 * \brief Mount a NTFS volume
86 tVFS_Node *NTFS_InitDevice(const char *Device, const char **Options)
91 disk = calloc( sizeof(tNTFS_Disk), 1 );
93 disk->FD = VFS_Open(Device, VFS_OPENFLAG_READ);
99 VFS_ReadAt(disk->FD, 0, 512, &bs);
102 Log_Debug("FS_NTFS", "Jump = %02x%02x%02x",
106 Log_Debug("FS_NTFS", "SystemID = %02x%02x%02x%02x%02x%02x%02x%02x (%8C)",
107 bs.SystemID[0], bs.SystemID[1], bs.SystemID[2], bs.SystemID[3],
108 bs.SystemID[4], bs.SystemID[5], bs.SystemID[6], bs.SystemID[7],
111 Log_Debug("FS_NTFS", "BytesPerSector = %i", bs.BytesPerSector);
112 Log_Debug("FS_NTFS", "SectorsPerCluster = %i", bs.SectorsPerCluster);
113 Log_Debug("FS_NTFS", "MediaDescriptor = 0x%x", bs.MediaDescriptor);
114 Log_Debug("FS_NTFS", "SectorsPerTrack = %i", bs.SectorsPerTrack);
115 Log_Debug("FS_NTFS", "Heads = %i", bs.Heads);
116 Log_Debug("FS_NTFS", "TotalSectorCount = 0x%llx", bs.TotalSectorCount);
117 Log_Debug("FS_NTFS", "MFTStart = 0x%llx", bs.MFTStart);
118 Log_Debug("FS_NTFS", "MFTMirrorStart = 0x%llx", bs.MFTMirrorStart);
119 Log_Debug("FS_NTFS", "ClustersPerMFTRecord = %i", bs.ClustersPerMFTRecord);
120 Log_Debug("FS_NTFS", "ClustersPerIndexRecord = %i", bs.ClustersPerIndexRecord);
121 Log_Debug("FS_NTFS", "SerialNumber = 0x%llx", bs.SerialNumber);
124 disk->ClusterSize = bs.BytesPerSector * bs.SectorsPerCluster;
125 disk->MFTBase = bs.MFTStart;
126 Log_Debug("NTFS", "Cluster Size = %i KiB", disk->ClusterSize/1024);
127 Log_Debug("NTFS", "MFT Base = %i", disk->MFTBase);
128 Log_Debug("NTFS", "TotalSectorCount = 0x%x", bs.TotalSectorCount);
130 if( bs.ClustersPerMFTRecord < 0 ) {
131 disk->MFTRecSize = 1 << (-bs.ClustersPerMFTRecord);
134 disk->MFTRecSize = bs.ClustersPerMFTRecord * disk->ClusterSize;
136 //NTFS_DumpEntry(disk, 0); // $MFT
137 //NTFS_DumpEntry(disk, 3); // $VOLUME
139 disk->InodeCache = Inode_GetHandle();
141 disk->MFTDataAttr = NULL;
142 disk->MFTDataAttr = NTFS_GetAttrib(disk, 0, NTFS_FileAttrib_Data, "", 0);
143 //NTFS_DumpEntry(disk, 5); // .
145 disk->RootDir.I30Root = NTFS_GetAttrib(disk, 5, NTFS_FileAttrib_IndexRoot, "$I30", 0);
146 disk->RootDir.I30Allocation = NTFS_GetAttrib(disk, 5, NTFS_FileAttrib_IndexAllocation, "$I30", 0);
147 disk->RootDir.Node.Inode = 5; // MFT Ent #5 is filesystem root
148 disk->RootDir.Node.ImplPtr = disk;
149 disk->RootDir.Node.Type = &gNTFS_DirType;
150 disk->RootDir.Node.Flags = VFS_FFLAG_DIRECTORY;
152 disk->RootDir.Node.UID = 0;
153 disk->RootDir.Node.GID = 0;
155 disk->RootDir.Node.NumACLs = 1;
156 disk->RootDir.Node.ACLs = &gVFS_ACL_EveryoneRX;
160 // Read from allocation
161 char buf[disk->ClusterSize];
162 size_t len = NTFS_ReadAttribData(disk->RootDir.I30Allocation, 0, sizeof(buf), buf);
163 Debug_HexDump("RootDir allocation", buf, len);
167 return &disk->RootDir.Node;
171 * \brief Unmount an NTFS Disk
173 void NTFS_Unmount(tVFS_Node *Node)
175 tNTFS_Disk *Disk = Node->ImplPtr;
180 tNTFS_FILE_Header *NTFS_GetMFT(tNTFS_Disk *Disk, Uint32 MFTEntry)
182 void *ret = malloc( Disk->MFTRecSize );
184 Log_Warning("FS_NTFS", "malloc() fail!");
188 // NOTE: The MFT is a file, and can get fragmented
189 if( !Disk->MFTDataAttr ) {
190 VFS_ReadAt( Disk->FD,
191 Disk->MFTBase * Disk->ClusterSize + MFTEntry * Disk->MFTRecSize,
196 NTFS_ReadAttribData(Disk->MFTDataAttr, MFTEntry * Disk->MFTRecSize, Disk->MFTRecSize, ret);
202 void NTFS_ReleaseMFT(tNTFS_Disk *Disk, Uint32 MFTEntry, tNTFS_FILE_Header *Entry)
207 static inline Uint64 _getVariableLengthInt(const void *Ptr, int Length, int bExtend)
209 const Uint8 *data = Ptr;
211 for( int i = 0; i < Length; i ++ )
212 bits |= (Uint64)data[i] << (i*8);
213 if( bExtend && Length && data[Length-1] & 0x80 ) {
214 for( int i = Length; i < 8; i ++ )
215 bits |= 0xFF << (i*8);
220 const void *_GetDataRun(const void *ptr, const void *limit, Uint64 LastLCN, Uint64 *Count, Uint64 *LCN)
224 LOG("Clean end of list");
228 const Uint8 *data = ptr;
231 Uint8 ofsSize = data[0] >> 4;
232 Uint8 lenSize = data[0] & 0xF;
233 LOG("ofsSize = %i, lenSize = %i", ofsSize, lenSize);
236 if( lenSize > 8 || lenSize < 1 )
238 if( data + 1 + ofsSize + lenSize > (const Uint8*)limit )
242 *Count = _getVariableLengthInt(data + 1, lenSize, 0);
245 *LCN = LastLCN + (Sint64)_getVariableLengthInt(data + 1 + lenSize, ofsSize, 1);
248 return data + 1 + ofsSize + lenSize;
251 tNTFS_Attrib *NTFS_GetAttrib(tNTFS_Disk *Disk, Uint32 MFTEntry, int Type, const char *Name, int DesIdx)
253 ENTER("pDisk xMFTEntry xType sName iDesIdx",
254 Disk, MFTEntry, Type, Name, DesIdx);
256 // TODO: Scan cache of attributes
259 tNTFS_FILE_Header *hdr = NTFS_GetMFT(Disk, MFTEntry);
260 LOG("hdr = %p", hdr);
262 tNTFS_FILE_Attrib *attr;
263 for( size_t ofs = hdr->FirstAttribOfs; ofs < hdr->RecordSize; ofs += attr->Size )
265 attr = (void*)( (tVAddr)hdr + ofs );
267 if( ofs + 4 > hdr->RecordSize )
270 if( attr->Type == 0xFFFFFFFF )
272 // Sanity #2: Type,Size
273 if( ofs + 8 > hdr->RecordSize )
275 // Sanity #3: Reported size
276 if( attr->Size < sizeof(attr->Resident) )
278 // Sanity #4: Reported size fits
279 if( ofs + attr->Size > hdr->RecordSize )
282 // - Chceck if this attribute is the one requested
283 LOG("Type check %x == %x", attr->Type, Type);
284 if( attr->Type != Type )
287 LOG("Name check = '%s'", Name);
288 const void *name16 = (char*)attr + attr->NameOffset;
289 if( UTF16_CompareWithUTF8(attr->NameLength, name16, Name) != 0 )
292 LOG("Idx check %i", curIdx);
293 if( curIdx++ != DesIdx )
296 // - Construct (and cache) attribute description
297 ASSERT(attr->NameOffset % 1 == 0);
298 Uint16 *name16 = (Uint16*)attr + attr->NameOffset/2;
299 size_t namelen = UTF16_ConvertToUTF8(0, NULL, attr->NameLength, name16);
300 size_t edatalen = (attr->NonresidentFlag ? 0 : attr->Resident.AttribLen*4);
301 tNTFS_Attrib *ret = malloc( sizeof(tNTFS_Attrib) + namelen + 1 + edatalen );
306 if( attr->NonresidentFlag )
307 ret->Name = (void*)(ret + 1);
309 ret->ResidentData = ret + 1;
310 ret->Name = (char*)ret->ResidentData + edatalen;
314 ret->Type = attr->Type;
315 UTF16_ConvertToUTF8(namelen+1, ret->Name, attr->NameLength, name16);
316 ret->IsResident = !(attr->NonresidentFlag);
318 LOG("Creating with %x '%s'", ret->Type, ret->Name);
320 if( attr->NonresidentFlag )
322 ret->DataSize = attr->NonResident.RealSize;
323 ret->NonResident.CompressionUnitL2Size = attr->NonResident.CompressionUnitSize;
324 ret->NonResident.FirstPopulatedCluster = attr->NonResident.StartingVCN;
326 const char *limit = (char*)attr + attr->Size;
328 const char *datarun = (char*)attr + attr->NonResident.DataRunOfs;
329 while( (datarun = _GetDataRun(datarun, limit, 0, NULL, NULL)) )
331 LOG("nruns = %i", nruns);
332 // Allocate data runs
333 ret->NonResident.nRuns = nruns;
334 ret->NonResident.Runs = malloc( sizeof(tNTFS_AttribDataRun) * nruns );
336 datarun = (char*)attr + attr->NonResident.DataRunOfs;
338 while( datarun && i < nruns )
340 tNTFS_AttribDataRun *run = &ret->NonResident.Runs[i];
341 datarun = _GetDataRun(datarun,limit, lastLCN, &run->Count, &run->LCN);
342 LOG("Run %i: %llx+%llx", i, run->LCN, run->Count);
349 ret->DataSize = edatalen;
350 memcpy(ret->ResidentData, (char*)attr + attr->Resident.AttribOfs, edatalen);
357 NTFS_ReleaseMFT(Disk, MFTEntry, hdr);
362 size_t NTFS_ReadAttribData(tNTFS_Attrib *Attrib, Uint64 Offset, size_t Length, void *Buffer)
366 if( Offset >= Attrib->DataSize )
368 if( Length > Attrib->DataSize )
369 Length = Attrib->DataSize;
370 if( Offset + Length > Attrib->DataSize )
371 Length = Attrib->DataSize - Offset;
373 if( Attrib->IsResident )
375 memcpy(Buffer, Attrib->ResidentData, Length);
381 tNTFS_Disk *Disk = Attrib->Disk;
382 Uint64 first_cluster = Offset / Disk->ClusterSize;
383 size_t cluster_ofs = Offset % Disk->ClusterSize;
384 if( first_cluster < Attrib->NonResident.FirstPopulatedCluster ) {
385 Log_Warning("NTFS", "TODO: Ofs < FirstVCN");
387 first_cluster -= Attrib->NonResident.FirstPopulatedCluster;
388 if( Attrib->NonResident.CompressionUnitL2Size )
391 Log_Warning("NTFS", "Compression unsupported");
392 // NOTE: Compressed blocks show up in pairs of runs
393 // - The first contains the compressed data
394 // - The second is a placeholder 'sparse' (LCN=0) to align to the compression unit
398 // Iterate through data runs until the desired run is located
399 for( int i = 0; i < Attrib->NonResident.nRuns && Length; i ++ )
401 tNTFS_AttribDataRun *run = &Attrib->NonResident.Runs[i];
402 if( first_cluster > run->Count ) {
403 first_cluster -= run->Count;
406 size_t avail_bytes = (run->Count-first_cluster)*Disk->ClusterSize - cluster_ofs;
407 if( avail_bytes > Length )
408 avail_bytes = Length;
409 // Read from this extent
410 if( run->LCN == 0 ) {
411 memset(Buffer, 0, avail_bytes);
415 (run->LCN + first_cluster)*Disk->ClusterSize + cluster_ofs,
420 Length -= avail_bytes;
421 Buffer += avail_bytes;
433 * \brief Dumps a MFT Entry
435 void NTFS_DumpEntry(tNTFS_Disk *Disk, Uint32 Entry)
437 tNTFS_FILE_Attrib *attr;
440 tNTFS_FILE_Header *hdr = malloc( Disk->MFTRecSize );
442 Log_Warning("FS_NTFS", "malloc() fail!");
446 VFS_ReadAt( Disk->FD,
447 Disk->MFTBase * Disk->ClusterSize + Entry * Disk->MFTRecSize,
451 Log_Debug("FS_NTFS", "MFT Entry #%i", Entry);
452 Log_Debug("FS_NTFS", "- Magic = 0x%08x (%4C)", hdr->Magic, &hdr->Magic);
453 Log_Debug("FS_NTFS", "- UpdateSequenceOfs = 0x%x", hdr->UpdateSequenceOfs);
454 Log_Debug("FS_NTFS", "- UpdateSequenceSize = 0x%x", hdr->UpdateSequenceSize);
455 Log_Debug("FS_NTFS", "- LSN = 0x%x", hdr->LSN);
456 Log_Debug("FS_NTFS", "- SequenceNumber = %i", hdr->SequenceNumber);
457 Log_Debug("FS_NTFS", "- HardLinkCount = %i", hdr->HardLinkCount);
458 Log_Debug("FS_NTFS", "- FirstAttribOfs = 0x%x", hdr->FirstAttribOfs);
459 Log_Debug("FS_NTFS", "- Flags = 0x%x", hdr->Flags);
460 Log_Debug("FS_NTFS", "- RecordSize = 0x%x", hdr->RecordSize);
461 Log_Debug("FS_NTFS", "- RecordSpace = 0x%x", hdr->RecordSpace);
462 Log_Debug("FS_NTFS", "- Reference = 0x%llx", hdr->Reference);
463 Log_Debug("FS_NTFS", "- NextAttribID = 0x%04x", hdr->NextAttribID);
465 attr = (void*)( (char*)hdr + hdr->FirstAttribOfs );
467 while( (tVAddr)attr < (tVAddr)hdr + hdr->RecordSize )
469 if(attr->Type == 0xFFFFFFFF) break;
470 Log_Debug("FS_NTFS", "- Attribute %i", i ++);
471 Log_Debug("FS_NTFS", " > Type = 0x%x", attr->Type);
472 Log_Debug("FS_NTFS", " > Size = 0x%x", attr->Size);
473 Log_Debug("FS_NTFS", " > ResidentFlag = 0x%x", attr->NonresidentFlag);
474 Log_Debug("FS_NTFS", " > NameLength = %i", attr->NameLength);
475 Log_Debug("FS_NTFS", " > NameOffset = 0x%x", attr->NameOffset);
476 Log_Debug("FS_NTFS", " > Flags = 0x%x", attr->Flags);
477 Log_Debug("FS_NTFS", " > AttributeID = 0x%x", attr->AttributeID);
479 Uint16 *name16 = (void*)((char*)attr + attr->NameOffset);
480 size_t len = UTF16_ConvertToUTF8(0, NULL, attr->NameLength, name16);
482 UTF16_ConvertToUTF8(len+1, name, attr->NameLength, name16);
483 Log_Debug("FS_NTFS", " > Name = '%s'", name);
485 if( !attr->NonresidentFlag ) {
486 Log_Debug("FS_NTFS", " > AttribLen = 0x%x", attr->Resident.AttribLen);
487 Log_Debug("FS_NTFS", " > AttribOfs = 0x%x", attr->Resident.AttribOfs);
488 Log_Debug("FS_NTFS", " > IndexedFlag = 0x%x", attr->Resident.IndexedFlag);
489 Debug_HexDump("FS_NTFS",
490 (void*)( (tVAddr)attr + attr->Resident.AttribOfs ),
491 attr->Resident.AttribLen
495 Log_Debug("FS_NTFS", " > StartingVCN = 0x%llx", attr->NonResident.StartingVCN);
496 Log_Debug("FS_NTFS", " > LastVCN = 0x%llx", attr->NonResident.LastVCN);
497 Log_Debug("FS_NTFS", " > DataRunOfs = 0x%x", attr->NonResident.DataRunOfs);
498 Log_Debug("FS_NTFS", " > CompressionUnitSize = 0x%x", attr->NonResident.CompressionUnitSize);
499 Log_Debug("FS_NTFS", " > AllocatedSize = 0x%llx", attr->NonResident.AllocatedSize);
500 Log_Debug("FS_NTFS", " > RealSize = 0x%llx", attr->NonResident.RealSize);
501 Log_Debug("FS_NTFS", " > InitiatedSize = 0x%llx", attr->NonResident.InitiatedSize);
502 Debug_HexDump("FS_NTFS",
503 (char*)attr + attr->NonResident.DataRunOfs,
504 attr->Size - attr->NonResident.DataRunOfs
508 attr = (void*)( (tVAddr)attr + attr->Size );