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,
46 tNTFS_Disk gNTFS_Disks;
50 * \brief Installs the NTFS driver
52 int NTFS_Install(char **Arguments)
54 VFS_AddDriver( &gNTFS_FSInfo );
59 * \brief Detect if a volume is NTFS
61 int NTFS_Detect(int FD)
64 VFS_ReadAt(FD, 0, 512, &bs);
66 if( bs.BytesPerSector == 0 || (bs.BytesPerSector & 511) )
69 Uint64 ncluster = bs.TotalSectorCount / bs.SectorsPerCluster;
70 if( bs.MFTStart >= ncluster || bs.MFTMirrorStart >= ncluster )
73 if( memcmp(bs.SystemID, "NTFS ", 8) != 0 )
80 * \brief Mount a NTFS volume
82 tVFS_Node *NTFS_InitDevice(const char *Device, const char **Options)
87 disk = calloc( sizeof(tNTFS_Disk), 1 );
89 disk->FD = VFS_Open(Device, VFS_OPENFLAG_READ);
95 Log_Debug("FS_NTFS", "&bs = %p", &bs);
96 VFS_ReadAt(disk->FD, 0, 512, &bs);
98 Log_Debug("FS_NTFS", "Jump = %02x%02x%02x",
102 Log_Debug("FS_NTFS", "SystemID = %02x%02x%02x%02x%02x%02x%02x%02x (%8C)",
103 bs.SystemID[0], bs.SystemID[1], bs.SystemID[2], bs.SystemID[3],
104 bs.SystemID[4], bs.SystemID[5], bs.SystemID[6], bs.SystemID[7],
107 Log_Debug("FS_NTFS", "BytesPerSector = %i", bs.BytesPerSector);
108 Log_Debug("FS_NTFS", "SectorsPerCluster = %i", bs.SectorsPerCluster);
109 Log_Debug("FS_NTFS", "MediaDescriptor = 0x%x", bs.MediaDescriptor);
110 Log_Debug("FS_NTFS", "SectorsPerTrack = %i", bs.SectorsPerTrack);
111 Log_Debug("FS_NTFS", "Heads = %i", bs.Heads);
112 Log_Debug("FS_NTFS", "TotalSectorCount = 0x%llx", bs.TotalSectorCount);
113 Log_Debug("FS_NTFS", "MFTStart = 0x%llx", bs.MFTStart);
114 Log_Debug("FS_NTFS", "MFTMirrorStart = 0x%llx", bs.MFTMirrorStart);
115 Log_Debug("FS_NTFS", "ClustersPerMFTRecord = %i", bs.ClustersPerMFTRecord);
116 Log_Debug("FS_NTFS", "ClustersPerIndexRecord = %i", bs.ClustersPerIndexRecord);
117 Log_Debug("FS_NTFS", "SerialNumber = 0x%llx", bs.SerialNumber);
119 disk->ClusterSize = bs.BytesPerSector * bs.SectorsPerCluster;
120 Log_Debug("NTFS", "Cluster Size = %i KiB", disk->ClusterSize/1024);
121 disk->MFTBase = bs.MFTStart;
122 Log_Debug("NTFS", "MFT Base = %i", disk->MFTBase);
123 Log_Debug("NTFS", "TotalSectorCount = 0x%x", bs.TotalSectorCount);
125 if( bs.ClustersPerMFTRecord < 0 ) {
126 disk->MFTRecSize = 1 << (-bs.ClustersPerMFTRecord);
129 disk->MFTRecSize = bs.ClustersPerMFTRecord * disk->ClusterSize;
131 NTFS_DumpEntry(disk, 0); // $MFT
132 //NTFS_DumpEntry(disk, 3); // $VOLUME
134 disk->MFTDataAttr = NULL;
135 disk->MFTDataAttr = NTFS_GetAttrib(disk, 0, NTFS_FileAttrib_Data, "", 0);
136 NTFS_DumpEntry(disk, 5); // .
138 disk->RootDir.I30Root = NTFS_GetAttrib(disk, 5, NTFS_FileAttrib_IndexRoot, "$I30", 0);
139 disk->RootDir.I30Allocation = NTFS_GetAttrib(disk, 5, NTFS_FileAttrib_IndexAllocation, "$I30", 0);
140 disk->RootDir.Node.Inode = 5; // MFT Ent #5 is filesystem root
141 disk->RootDir.Node.ImplPtr = disk;
142 disk->RootDir.Node.Type = &gNTFS_DirType;
143 disk->RootDir.Node.Flags = VFS_FFLAG_DIRECTORY;
145 disk->RootDir.Node.UID = 0;
146 disk->RootDir.Node.GID = 0;
148 disk->RootDir.Node.NumACLs = 1;
149 disk->RootDir.Node.ACLs = &gVFS_ACL_EveryoneRX;
152 // Read from allocation
153 char buf[disk->ClusterSize];
154 size_t len = NTFS_ReadAttribData(disk->RootDir.I30Allocation, 0, sizeof(buf), buf);
155 Debug_HexDump("RootDir allocation", buf, len);
158 return &disk->RootDir.Node;
162 * \brief Unmount an NTFS Disk
164 void NTFS_Unmount(tVFS_Node *Node)
166 tNTFS_Disk *Disk = Node->ImplPtr;
171 tNTFS_FILE_Header *NTFS_GetMFT(tNTFS_Disk *Disk, Uint32 MFTEntry)
173 void *ret = malloc( Disk->MFTRecSize );
175 Log_Warning("FS_NTFS", "malloc() fail!");
179 // NOTE: The MFT is a file, and can get fragmented
180 if( !Disk->MFTDataAttr ) {
181 VFS_ReadAt( Disk->FD,
182 Disk->MFTBase * Disk->ClusterSize + MFTEntry * Disk->MFTRecSize,
187 NTFS_ReadAttribData(Disk->MFTDataAttr, MFTEntry * Disk->MFTRecSize, Disk->MFTRecSize, ret);
193 void NTFS_ReleaseMFT(tNTFS_Disk *Disk, Uint32 MFTEntry, tNTFS_FILE_Header *Entry)
198 static inline Uint64 _getVariableLengthInt(const void *Ptr, int Length, int bExtend)
200 const Uint8 *data = Ptr;
202 for( int i = 0; i < Length; i ++ )
203 bits |= (Uint64)data[i] << (i*8);
204 if( bExtend && Length && data[Length-1] & 0x80 ) {
205 for( int i = Length; i < 8; i ++ )
206 bits |= 0xFF << (i*8);
211 const void *_GetDataRun(const void *ptr, const void *limit, Uint64 LastLCN, Uint64 *Count, Uint64 *LCN)
215 LOG("Clean end of list");
219 const Uint8 *data = ptr;
222 Uint8 ofsSize = data[0] >> 4;
223 Uint8 lenSize = data[0] & 0xF;
224 LOG("ofsSize = %i, lenSize = %i", ofsSize, lenSize);
227 if( lenSize > 8 || lenSize < 1 )
229 if( data + 1 + ofsSize + lenSize > (const Uint8*)limit )
233 *Count = _getVariableLengthInt(data + 1, lenSize, 0);
236 *LCN = LastLCN + (Sint64)_getVariableLengthInt(data + 1 + lenSize, ofsSize, 1);
239 return data + 1 + ofsSize + lenSize;
242 tNTFS_Attrib *NTFS_GetAttrib(tNTFS_Disk *Disk, Uint32 MFTEntry, int Type, const char *Name, int DesIdx)
244 ENTER("pDisk xMFTEntry xType sName iDesIdx",
245 Disk, MFTEntry, Type, Name, DesIdx);
247 // TODO: Scan cache of attributes
250 tNTFS_FILE_Header *hdr = NTFS_GetMFT(Disk, MFTEntry);
251 LOG("hdr = %p", hdr);
253 tNTFS_FILE_Attrib *attr;
254 for( size_t ofs = hdr->FirstAttribOfs; ofs < hdr->RecordSize; ofs += attr->Size )
256 attr = (void*)( (tVAddr)hdr + ofs );
258 if( ofs + 4 > hdr->RecordSize )
261 if( attr->Type == 0xFFFFFFFF )
263 // Sanity #2: Type,Size
264 if( ofs + 8 > hdr->RecordSize )
266 // Sanity #3: Reported size
267 if( attr->Size < sizeof(attr->Resident) )
269 // Sanity #4: Reported size fits
270 if( ofs + attr->Size > hdr->RecordSize )
273 // - Chceck if this attribute is the one requested
274 LOG("Type check %x == %x", attr->Type, Type);
275 if( attr->Type != Type )
278 LOG("Name check = '%s'", Name);
279 const void *name16 = (char*)attr + attr->NameOffset;
280 if( UTF16_CompareWithUTF8(attr->NameLength, name16, Name) != 0 )
283 LOG("Idx check %i", curIdx);
284 if( curIdx++ != DesIdx )
287 // - Construct (and cache) attribute description
288 ASSERT(attr->NameOffset % 1 == 0);
289 Uint16 *name16 = (Uint16*)attr + attr->NameOffset/2;
290 size_t namelen = UTF16_ConvertToUTF8(0, NULL, attr->NameLength, name16);
291 size_t edatalen = (attr->NonresidentFlag ? 0 : attr->Resident.AttribLen*4);
292 tNTFS_Attrib *ret = malloc( sizeof(tNTFS_Attrib) + namelen + 1 + edatalen );
297 if( attr->NonresidentFlag )
298 ret->Name = (void*)(ret + 1);
300 ret->ResidentData = ret + 1;
301 ret->Name = (char*)ret->ResidentData + edatalen;
305 ret->Type = attr->Type;
306 UTF16_ConvertToUTF8(namelen+1, ret->Name, attr->NameLength, name16);
307 ret->IsResident = !(attr->NonresidentFlag);
309 LOG("Creating with %x '%s'", ret->Type, ret->Name);
311 if( attr->NonresidentFlag )
313 ret->DataSize = attr->NonResident.RealSize;
314 ret->NonResident.CompressionUnitL2Size = attr->NonResident.CompressionUnitSize;
315 ret->NonResident.FirstPopulatedCluster = attr->NonResident.StartingVCN;
317 const char *limit = (char*)attr + attr->Size;
319 const char *datarun = (char*)attr + attr->NonResident.DataRunOfs;
320 while( (datarun = _GetDataRun(datarun, limit, 0, NULL, NULL)) )
322 LOG("nruns = %i", nruns);
323 // Allocate data runs
324 ret->NonResident.nRuns = nruns;
325 ret->NonResident.Runs = malloc( sizeof(tNTFS_AttribDataRun) * nruns );
327 datarun = (char*)attr + attr->NonResident.DataRunOfs;
329 while( datarun && i < nruns )
331 tNTFS_AttribDataRun *run = &ret->NonResident.Runs[i];
332 datarun = _GetDataRun(datarun,limit, lastLCN, &run->Count, &run->LCN);
333 LOG("Run %i: %llx+%llx", i, run->LCN, run->Count);
340 memcpy(ret->ResidentData, (char*)attr + attr->Resident.AttribOfs, edatalen);
347 NTFS_ReleaseMFT(Disk, MFTEntry, hdr);
352 size_t NTFS_ReadAttribData(tNTFS_Attrib *Attrib, Uint64 Offset, size_t Length, void *Buffer)
354 if( Offset >= Attrib->DataSize )
356 if( Length > Attrib->DataSize )
357 Length = Attrib->DataSize;
358 if( Offset + Length > Attrib->DataSize )
359 Length = Attrib->DataSize - Offset;
361 if( Attrib->IsResident )
363 memcpy(Buffer, Attrib->ResidentData, Length);
369 tNTFS_Disk *Disk = Attrib->Disk;
370 Uint64 first_cluster = Offset / Disk->ClusterSize;
371 size_t cluster_ofs = Offset % Disk->ClusterSize;
372 if( first_cluster < Attrib->NonResident.FirstPopulatedCluster ) {
373 Log_Warning("NTFS", "TODO: Ofs < FirstVCN");
375 first_cluster -= Attrib->NonResident.FirstPopulatedCluster;
376 if( Attrib->NonResident.CompressionUnitL2Size )
379 Log_Warning("NTFS", "Compression unsupported");
380 // NOTE: Compressed blocks show up in pairs of runs
381 // - The first contains the compressed data
382 // - The second is a placeholder 'sparse' (LCN=0) to align to the compression unit
386 // Iterate through data runs until the desired run is located
387 for( int i = 0; i < Attrib->NonResident.nRuns && Length; i ++ )
389 tNTFS_AttribDataRun *run = &Attrib->NonResident.Runs[i];
390 if( first_cluster > run->Count ) {
391 first_cluster -= run->Count;
394 size_t avail_bytes = (run->Count-first_cluster)*Disk->ClusterSize - cluster_ofs;
395 if( avail_bytes > Length )
396 avail_bytes = Length;
397 // Read from this extent
398 if( run->LCN == 0 ) {
399 memset(Buffer, 0, avail_bytes);
403 (run->LCN + first_cluster)*Disk->ClusterSize + cluster_ofs,
408 Length -= avail_bytes;
409 Buffer += avail_bytes;
421 * \brief Dumps a MFT Entry
423 void NTFS_DumpEntry(tNTFS_Disk *Disk, Uint32 Entry)
425 tNTFS_FILE_Attrib *attr;
428 tNTFS_FILE_Header *hdr = malloc( Disk->MFTRecSize );
430 Log_Warning("FS_NTFS", "malloc() fail!");
434 VFS_ReadAt( Disk->FD,
435 Disk->MFTBase * Disk->ClusterSize + Entry * Disk->MFTRecSize,
439 Log_Debug("FS_NTFS", "MFT Entry #%i", Entry);
440 Log_Debug("FS_NTFS", "- Magic = 0x%08x (%4C)", hdr->Magic, &hdr->Magic);
441 Log_Debug("FS_NTFS", "- UpdateSequenceOfs = 0x%x", hdr->UpdateSequenceOfs);
442 Log_Debug("FS_NTFS", "- UpdateSequenceSize = 0x%x", hdr->UpdateSequenceSize);
443 Log_Debug("FS_NTFS", "- LSN = 0x%x", hdr->LSN);
444 Log_Debug("FS_NTFS", "- SequenceNumber = %i", hdr->SequenceNumber);
445 Log_Debug("FS_NTFS", "- HardLinkCount = %i", hdr->HardLinkCount);
446 Log_Debug("FS_NTFS", "- FirstAttribOfs = 0x%x", hdr->FirstAttribOfs);
447 Log_Debug("FS_NTFS", "- Flags = 0x%x", hdr->Flags);
448 Log_Debug("FS_NTFS", "- RecordSize = 0x%x", hdr->RecordSize);
449 Log_Debug("FS_NTFS", "- RecordSpace = 0x%x", hdr->RecordSpace);
450 Log_Debug("FS_NTFS", "- Reference = 0x%llx", hdr->Reference);
451 Log_Debug("FS_NTFS", "- NextAttribID = 0x%04x", hdr->NextAttribID);
453 attr = (void*)( (char*)hdr + hdr->FirstAttribOfs );
455 while( (tVAddr)attr < (tVAddr)hdr + hdr->RecordSize )
457 if(attr->Type == 0xFFFFFFFF) break;
458 Log_Debug("FS_NTFS", "- Attribute %i", i ++);
459 Log_Debug("FS_NTFS", " > Type = 0x%x", attr->Type);
460 Log_Debug("FS_NTFS", " > Size = 0x%x", attr->Size);
461 Log_Debug("FS_NTFS", " > ResidentFlag = 0x%x", attr->NonresidentFlag);
462 Log_Debug("FS_NTFS", " > NameLength = %i", attr->NameLength);
463 Log_Debug("FS_NTFS", " > NameOffset = 0x%x", attr->NameOffset);
464 Log_Debug("FS_NTFS", " > Flags = 0x%x", attr->Flags);
465 Log_Debug("FS_NTFS", " > AttributeID = 0x%x", attr->AttributeID);
467 Uint16 *name16 = (void*)((char*)attr + attr->NameOffset);
468 size_t len = UTF16_ConvertToUTF8(0, NULL, attr->NameLength, name16);
470 UTF16_ConvertToUTF8(len+1, name, attr->NameLength, name16);
471 Log_Debug("FS_NTFS", " > Name = '%s'", name);
473 if( !attr->NonresidentFlag ) {
474 Log_Debug("FS_NTFS", " > AttribLen = 0x%x", attr->Resident.AttribLen);
475 Log_Debug("FS_NTFS", " > AttribOfs = 0x%x", attr->Resident.AttribOfs);
476 Log_Debug("FS_NTFS", " > IndexedFlag = 0x%x", attr->Resident.IndexedFlag);
477 Debug_HexDump("FS_NTFS",
478 (void*)( (tVAddr)attr + attr->Resident.AttribOfs ),
479 attr->Resident.AttribLen
483 Log_Debug("FS_NTFS", " > StartingVCN = 0x%llx", attr->NonResident.StartingVCN);
484 Log_Debug("FS_NTFS", " > LastVCN = 0x%llx", attr->NonResident.LastVCN);
485 Log_Debug("FS_NTFS", " > DataRunOfs = 0x%x", attr->NonResident.DataRunOfs);
486 Log_Debug("FS_NTFS", " > CompressionUnitSize = 0x%x", attr->NonResident.CompressionUnitSize);
487 Log_Debug("FS_NTFS", " > AllocatedSize = 0x%llx", attr->NonResident.AllocatedSize);
488 Log_Debug("FS_NTFS", " > RealSize = 0x%llx", attr->NonResident.RealSize);
489 Log_Debug("FS_NTFS", " > InitiatedSize = 0x%llx", attr->NonResident.InitiatedSize);
490 Debug_HexDump("FS_NTFS",
491 (char*)attr + attr->NonResident.DataRunOfs,
492 attr->Size - attr->NonResident.DataRunOfs
496 attr = (void*)( (tVAddr)attr + attr->Size );