Beispiel #1
0
        }// ~public MBRScan( string physicalDriveName )-----------------------------------------

        public bool Scan(string physicalDriveName)
        {
            const uint SectorSize = 512;

            byte[] buff = new byte[512];
            uint   nBytesRead;


            // Create handle to physical drive
            SafeFileHandle shFile = WinApi.CreateFile(physicalDriveName, FileAccess.Read, FileShare.Read, (IntPtr)0, FileMode.Open, 0, (IntPtr)0);

            // - - - - - - - - - - - - main MBR - - - - - - - - - - - - - - - - - - - -
            WinApi.ReadFile(shFile, buff, SectorSize, out nBytesRead, (IntPtr)0);
            Mbr = new MBR(buff);
            if (Mbr.isProtective())
            {
                return(true);
            }
            // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ~mainMBR
            shFile.Close();
            //add to Sections primary sections
            for (int sectionNumber = 0; sectionNumber < 4; sectionNumber++)
            {
                if (Mbr[sectionNumber].Type == 0xF || Mbr[sectionNumber].Type == 0x5)
                {
                    ulong baseAddr = Mbr[sectionNumber].getFirstSector();
                    EbrScan(physicalDriveName, baseAddr, 0);
                }
                else
                {
                    Sections.Add(Mbr[sectionNumber]);
                }
            }
            return(false);
        }// -------------------------------------------------------------------- ~Scan()
Beispiel #2
0
        }// -------------------------------------------------------------------- ~Scan()

        private bool EbrScan(string physicalDriveName, ulong baseAddr, ulong relAddr)
        {
            const uint SectorSize = 512;

            byte[] buff = new byte[512];
            uint   nBytesRead;

            SafeFileHandle shFile = WinApi.CreateFile(physicalDriveName, FileAccess.Read, FileShare.ReadWrite, (IntPtr)0, FileMode.Open, 0, (IntPtr)0);

            if (shFile.IsInvalid)
            {
                return(true);
            }

            UInt64 addr  = (baseAddr + relAddr) * SectorSize;
            int    hPart = (int)(addr >> 32);
            uint   lPart = (uint)addr;

            WinApi.SetFilePointer(shFile, lPart, out hPart, (uint)SeekOrigin.Begin);
            if (shFile.IsInvalid)
            {
                return(true);
            }

            WinApi.ReadFile(shFile, buff, SectorSize, out nBytesRead, (IntPtr)0);
            if (shFile.IsInvalid)
            {
                return(true);
            }
            shFile.Close();

            EbrList.Add(new MBR(buff));  //ext


            Sections.Add(EbrList[EbrList.Count - 1][0]);
            uint fs = (uint)(baseAddr + relAddr + Sections[Sections.Count - 1].getFirstSector());

            Sections[Sections.Count - 1].setFirstSector(fs);

            if (EbrList[EbrList.Count - 1][1].Type != 0x0)
            {
                EbrScan(physicalDriveName, baseAddr, EbrList[EbrList.Count - 1][1].getFirstSector());
                return(false);
            }
            else
            {
                return(false);
            }
        }// ~EbrScan
Beispiel #3
0
        private void updateFatTable(SafeFileHandle shFile)
        {
	        UInt64 begFAT;
	        //вделяем буфер кратный размеру сектора
            int BuffSize = BPB.SizeOfFatCopy * BPB.BytesPerSector;
	        Byte[] buff = new Byte[BuffSize];
	        UInt32 dwBytesRead;
	        int nFatElement;

	        //-----------------------------------------------------------------------------------
         
	        //address of Fat table
	        begFAT = ADDRESS + (ulong)BPB.SizeOfReserve * (ulong)BPB.BytesPerSector;

            int hPart = (int)(begFAT >> 32);
            uint lPart = (uint)begFAT;
	        //-----------------------------------------------------------------------------------

	        //--смещаемся и читаем фат таблицу в память---
	        WinApi.SetFilePointer(shFile, lPart, out hPart,  (uint)SeekOrigin.Begin);
            if (shFile.IsInvalid)
            {
                throw new InvalidDataException();
            }

	        try
            {
                WinApi.ReadFile(shFile, buff, (uint)BuffSize, out dwBytesRead, (IntPtr)0);
            }
            catch (FileLoadException ex)
            {
                ex.ToString();//!!!!!!!!!!!!!!!!! shit
            }
	      

	        //-----------------------------------------------------------------------------------

	        //заполняем таблицу фат
	      
		        nFatElement = (BPB.SizeOfFatCopy * BPB.BytesPerSector) / 4;
		        FAT_TABLE = new int[nFatElement];
		        for (int i = 0, j = 0; i < nFatElement; i++, j += 4)
		        {
			        FAT_TABLE[i] = BitConverter.ToInt32(buff, j);
		        }
	        
	        //------------------------------------------------------------------------------------
        }
        //ISystemic
        //тип сенгментації

        //-------------------------------------------------------Constructor(string)------------------
        public PhysicalDrive(string driveName)
        {
            const uint SectorSize = 512;

            byte[] buff = new byte[512];
            uint   nBytesRead;

            name = driveName;
            SafeFileHandle shFile = WinApi.CreateFile(driveName, FileAccess.Read, FileShare.Read, (IntPtr)0, FileMode.Open, 0, (IntPtr)0);

            if (shFile.IsInvalid)
            {
                throw new DriveNotFoundException("Drive" + driveName + "is not avalible");
            }

            WinApi.ReadFile(shFile, buff, SectorSize, out nBytesRead, (IntPtr)0);
            if (shFile.IsInvalid)
            {
                throw new InvalidDataException("Reading data form " + driveName + " is not avalible");
            }

            //---------------------------------------------------
            MBR protectiveMbr = new MBR(buff);

            if (protectiveMbr.isProtective())
            {
                Scanner = new GPTScan(name);
            }
            else
            {
                Scanner = new MBRScan(name);
            }
            //---------------------------------------------------

            //---------------------------------------------------
            for (int i = 0; i < Scanner.Length; i++)
            {
                volumes.Add(new Volume(driveName, Scanner[i]));
            }
            //---------------------------------------------------

            shFile.Close();
        }
Beispiel #5
0
        public MBRScan(string physicalDriveName)
        {
            const uint SectorSize = 512;

            byte[] buff = new byte[512];
            uint   nBytesRead;


            // Create handle to physical drive
            SafeFileHandle shFile = WinApi.CreateFile(physicalDriveName, FileAccess.Read, FileShare.Read, (IntPtr)0, FileMode.Open, 0, (IntPtr)0);

            if (shFile.IsInvalid)
            {
                throw new DriveNotFoundException(physicalDriveName + " not found");
            }


            // - - - - - - - - - - - - main MBR - - - - - - - - - - - - - - - - - - - -
            WinApi.ReadFile(shFile, buff, SectorSize, out nBytesRead, (IntPtr)0);
            Mbr = new MBR(buff);
            // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ~mainMBR
            shFile.Close();
            //add to Sections primary sections
            for (int sectionNumber = 0; sectionNumber < 4; sectionNumber++)
            {
                if (Mbr[sectionNumber].Type == 0xF || Mbr[sectionNumber].Type == 0x5)
                {
                    ulong baseAddr = Mbr[sectionNumber].getFirstSector();
                    EbrScan(physicalDriveName, baseAddr, 0);
                }
                else
                {
                    Sections.Add(Mbr[sectionNumber]);
                }
            }
        }// ~public MBRScan( string physicalDriveName )-----------------------------------------
        public GPTScan(string physicalDriveName)
        {
            const uint SectorSize = 512;

            byte[] buff = new byte[512];
            uint   nBytesRead;

            SafeFileHandle shFile = WinApi.CreateFile(physicalDriveName, FileAccess.Read, FileShare.Read, (IntPtr)0, FileMode.Open, 0, (IntPtr)0);

            //LBA0
            WinApi.ReadFile(shFile, buff, 512, out nBytesRead, (IntPtr)0);

            //LBA1 ---------------------------HEADER----------------------------------------------
            Mbr = new MBR(buff);

            UInt64 addr  = 512;
            int    hPart = (int)(addr >> 32);
            uint   lPart = (uint)addr;

            WinApi.SetFilePointer(shFile, lPart, out hPart, (uint)SeekOrigin.Begin);
            if (shFile.IsInvalid)
            {
                throw new InvalidDataException();
            }

            try
            {
                WinApi.ReadFile(shFile, buff, SectorSize, out nBytesRead, (IntPtr)0);
            }
            catch (FileLoadException ex)
            {
                ex.ToString();//!!!!!!!!!!!!!!!!! shit
            }

            Header = new GPTHeader(buff);
            //---------------------------------------------------------------------------- ~Header


            //LBA2...X
            for (uint i = 0; i < Header.getNumberOfPartitionEntries() / (SectorSize / Header.getSizeOfPartitionEntry()); i++)
            {
                addr += 512;
                hPart = (int)(addr >> 32);
                lPart = (uint)addr;
                WinApi.SetFilePointer(shFile, lPart, out hPart, (uint)SeekOrigin.Begin);

                WinApi.ReadFile(shFile, buff, 512, out nBytesRead, (IntPtr)0);

                int offset = 0;//офсет внутри сектора
                for (int j = 0; j < 512 / Header.getSizeOfPartitionEntry(); j++)
                {
                    //---Анализируем гуид на предмет неиспользуемой записи
                    byte[] guidBuff = new byte [16];
                    for (int guidIndex = 0; guidIndex < 16; guidIndex++)
                    {
                        guidBuff[guidIndex] = buff[guidIndex + offset];
                    }
                    Guid guid = new Guid(guidBuff);
                    //----------------------------------------------------------
                    if (!guid.Equals(Guid.Empty))
                    {
                        Sections.Add(new GPTSection(buff, offset));
                    }
                    offset += (int)Header.getSizeOfPartitionEntry();
                }
            }
            shFile.Close();
        }//GPTScan( string PhysicalDriveName )
Beispiel #7
0
        public Volume(string physicalDriveName, ISection section)
        {
            //-----------inits from section-------------
            beginAdders         = section.getFirstSector();
            size                = section.getSize();
            uniquePartitionGuid = section.getUniquePartitionGuid();
            partitionTypeGuid   = section.getPartitionTypeGuid();
            fileSystemType      = section.getFileSystemType();
            uniquePartitionGuid = section.getUniquePartitionGuid();
            partitionTypeGuid   = section.getPartitionTypeGuid();
            //------------------------------------------


            //-------------------------------------BOOT SECTOR---------------------------------------------
            const uint SectorSize = 512;

            byte[] buff = new byte[512];
            uint   nBytesRead;

            SafeFileHandle shFile = WinApi.CreateFile(physicalDriveName, FileAccess.Read, FileShare.Read, (IntPtr)0, FileMode.Open, 0, (IntPtr)0);

            if (shFile.IsInvalid)
            {
                throw new IOException();
            }

            UInt64 addr  = beginAdders * SectorSize;
            int    hPart = (int)(addr >> 32);
            uint   lPart = (uint)addr;

            WinApi.SetFilePointer(shFile, lPart, out hPart, (uint)SeekOrigin.Begin);
            if (shFile.IsInvalid)
            {
                throw new IOException();
            }

            //read boot
            WinApi.ReadFile(shFile, buff, SectorSize, out nBytesRead, (IntPtr)0);
            if (shFile.IsInvalid)
            {
                throw new IOException();
            }

            // def common fields
            Boot boot = new Boot(buff);

            // ------------------------------------------------------------------------------------------------- BOOT SECTOR

            // ------------------- init from boot ----------------------------------
            serialNumber = (ulong)boot.SerialNumber;

            // ----------------------------------------------------------------------


            // -------- get name by means of guid or serial number ------------------
            if (section.getUniquePartitionGuid() == Guid.Empty)
            {
                name = getDiskLetter(serialNumber);
            }
            else
            {
                name         = getDiskLetter(uniquePartitionGuid);
                serialNumber = 0U;
            }
            //-------------------------------------------------------------- ~ getName


            //------------------------------------------------------------------------------------- FILL BOOT SECTOR


            shFile.Close();
        } // ~Volume( string physicalDriveName, ISection section )