Ejemplo n.º 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()
Ejemplo n.º 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
Ejemplo n.º 3
0
        public Fat32(string physicalDriveName, ulong relativeAddress, BootFat32 BPB)
        {
	       
	        //---смещаемся на логический диск который выбрали---
	        //-----------чтение boot-сектора--------------------
            //HANDLE hFile = CreateFile(hardDrive, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);

            //addr.QuadPart = ((unsigned __int64)relativeAddress) * 512;
            //SetFilePointer(hFile, addr.LowPart, &addr.HighPart, FILE_BEGIN);
            //checkError();

            //BYTE *bootSector = readSector(hFile);
            //checkError();
            ////----------проверка сигнатуры NTFS-------------------------------------
            //char *oem = (char*)&bootSector[0x3];
            //if (oem[0] == 'N' && oem[1] == 'T' && oem[2] == 'F' && oem[3] == 'S')
            //{
            //    cout << "Current logical disk have NTFS!!!\n";
            //    system("pause");
            //    return;
            //}

            ////Заполняем поля структуры
            //BPB = fillBootSector(bootSector);
            ////----------------------------------------------------------------------

            ////определение типа фат
            //type = getTypeOfFAT(BPB);
            ////--------------------------------------------------------------------------

            SafeFileHandle shFile = WinApi.CreateFile(physicalDriveName, FileAccess.Read, FileShare.Read, (IntPtr)0, FileMode.Open, 0, (IntPtr)0);
            if (shFile.IsInvalid)
                throw new IOException();

	        //загружаем фат таблицу в память
	        updateFatTable(shFile);
	        //--------------------------------------------------------------------------

	        //-------------Рут каталог------------------------------------------------
		    ADDRESS_ROOT = relativeAddress + BPB->SizeOfReserve + (BPB.NumOfFATcpy * BPB.sizeOfFATcpy) + ((BPB.begRoot - 2) * BPB.SecPerCluster)) * BPB.BytsPerSector;
	        //-------------------------------------------------------------

	        updateRootSize(shFile);
	        updateRootElem(hFile);
	        CloseHandle(hFile);
}
        //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();
        }
Ejemplo n.º 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 )-----------------------------------------
Ejemplo n.º 6
0
        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 )
Ejemplo n.º 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 )