Пример #1
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
Пример #2
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);
		        }
	        
	        //------------------------------------------------------------------------------------
        }
Пример #3
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 )
Пример #4
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 )