/// <summary> /// Reads a number of blocks from the partition /// </summary> /// <param name="aBlockNo">Start block number</param> /// <param name="aBlockCount">Number of blocks</param> /// <param name="aData">Buffer to write to</param> public override void ReadBlock(UInt64 aBlockNo, UInt32 aBlockCount, byte[] aData) { UInt64 xHostBlockNo = mStartingSector + aBlockNo; CheckBlockNo(xHostBlockNo, aBlockCount); mHost.ReadBlock(xHostBlockNo, aBlockCount, aData); }
/// <summary> /// Reads the specified number of blocks from the specified partition /// </summary> /// <param name="aBlockNo">Starting block number</param> /// <param name="aBlockCount">Number of blocks to read</param> /// <param name="aData">byte[] array to read to</param> public override void ReadBlock(ulong aBlockNo, ulong aBlockCount, ref byte[] aData) { UInt64 HostBlockNumber = StartingSector + aBlockNo; CheckBlockNo(HostBlockNumber, aBlockCount); HostDevice.ReadBlock(HostBlockNumber, aBlockCount, ref aData); }
public override void ReadBlock(ulong blockNo, ulong blockCount, ref byte[] data) { CheckDataSize(data, blockCount); var hostBlockNo = StartingSector + blockNo; CheckBlockNo(hostBlockNo, blockCount); Host.ReadBlock(hostBlockNo, blockCount, ref data); }
public static bool BlockDeviceContainsExt2(BlockDevice aDevice) { if (aDevice.BlockCount > 3) { byte[] xBuffer = new byte[aDevice.BlockSize]; // todo: implement better detection aDevice.ReadBlock(2, xBuffer); bool xResult = (xBuffer[56] == 0x53 && xBuffer[57] == 0xEF); return(xResult); } return(false); }
private byte BlockReadByte(long offset) { var block = offset / BlockDevice.BlockSize; if (_bufferOffset != block) { _buffer = BlockDevice.ReadBlock(block); _bufferOffset = block; } var localOffset = offset - (block * BlockDevice.BlockSize); return(_buffer[localOffset]); }
private static List <Device> getPartitions(BlockDevice xBlockDev) { if (xBlockDev == null) { return(default(List <Device>)); } List <Device> partitionList = new List <Device>(); int xContentsIndex = 0x1BE; //DebugUtil.SendMessage("MBT", "Found Device"); byte[] xBlockContents = new byte[xBlockDev.BlockSize]; xBlockDev.ReadBlock(0, xBlockContents); // detecting whether MBT or not //DebugUtil.SendNumber("MBT", "xBlockDev.BlockSize", xBlockDev.BlockSize, 32); //DebugUtil.SendDoubleNumber("MBT", "Last bytes of block", xBlockContents[510], 8, xBlockContents[511], 8); if (!(xBlockContents[xBlockDev.BlockSize - 2] == 0x55 && xBlockContents[xBlockDev.BlockSize - 1] == 0xAA)) { //DebugUtil.SendMessage("MBT", "Does not contain MBR"); //Hardware.DebugUtil.SendATA_BlockReceived(255, 255, 0, xBlockContents); return(default(List <Device>)); } for (byte j = 0; j < 4; j++) { //DebugUtil.SendNumber("MBT", "Partition Status", xBlockContents[xContentsIndex], 8); if (!(xBlockContents[xContentsIndex] == 0x80 || xBlockContents[xContentsIndex] == 0)) { xContentsIndex += 16; continue; } xContentsIndex += 8; uint xStart = BitConverter.ToUInt32(xBlockContents, xContentsIndex); xContentsIndex += 4; uint xLength = BitConverter.ToUInt32(xBlockContents, xContentsIndex); xContentsIndex += 4; if (xStart > 0 && xLength > 0) { //DebugUtil.SendDoubleNumber("MBT", "Entry Found. Start, Length in blocks", xStart, 32, xLength, 32); xStart += 2; Console.WriteLine("Add Partition to Device list"); partitionList.Add(new MBTPartition(xBlockDev, xStart, xLength, "Partition")); //Causes System Fault on the HTC Shift! //DebugUtil.SendMessage("MBT", "FoundPartition"); } } return(partitionList); }
private void BlockWriteOffsetByte(long offset, byte b) { var block = offset / BlockDevice.BlockSize; if (_readBufferOffset != block) { _readBuffer = BlockDevice.ReadBlock(block); _readBufferOffset = block; } var localOffset = offset - (block * BlockDevice.BlockSize); _readBuffer[localOffset] = b; BlockDevice.WriteBlock(block, _readBuffer); }
private void BlockWriteOffsetBytes(long offset, byte[] b) { var block = offset / BlockDevice.BlockSize; if (_readBufferOffset != block) { _readBuffer = BlockDevice.ReadBlock(block); _readBufferOffset = block; } var localOffset = offset - (block * BlockDevice.BlockSize); foreach (var b1 in b) { _readBuffer[localOffset++] = b1; } BlockDevice.WriteBlock(block, _readBuffer); }
/// <summary> /// Create Partition. /// </summary> /// <param name="size">Size in MB.</param> /// <exception cref="ArgumentOutOfRangeException">Thrown if start / end is smaller then 0.</exception> /// <exception cref="ArgumentException">Thrown if end is smaller or equal to start.</exception> /// <exception cref="NotImplementedException">Thrown if partition type is GPT.</exception> public void CreatePartition(int size) { if (size == 0 | size < 0) { throw new ArgumentException("size"); } if (GPT.IsGPTPartition(Host)) { throw new Exception("Creating partitions with GPT style not yet supported!"); } int location; int startingSector = 63; uint amountOfSectors = (uint)(size * 1024 * 1024 / 512); //TODO: Check if partition is too big if (Partitions.Count == 0) { location = 446; startingSector = 63; } else if (Partitions.Count == 1) { location = 462; startingSector = (int)(Partitions[0].Host.BlockSize + Partitions[0].Host.BlockCount); } else if (Partitions.Count == 2) { location = 478; } else if (Partitions.Count == 3) { location = 494; } else { throw new NotImplementedException("Extended partitons not yet supported."); } //Create MBR var mbrData = new byte[512]; Host.ReadBlock(0, 1, ref mbrData); mbrData[location + 0] = 0x80; //bootable mbrData[location + 1] = 0x1; //starting head mbrData[location + 2] = 0; //Starting sector mbrData[location + 3] = 0x0; //Starting Cylinder mbrData[location + 4] = 83; //normal partition mbrData[location + 5] = 0xFE; //ending head mbrData[location + 6] = 0x3F; //Ending Sector mbrData[location + 7] = 0x40; //Ending Cylinder //Starting Sector byte[] startingSectorBytes = BitConverter.GetBytes(startingSector); mbrData[location + 8] = startingSectorBytes[0]; mbrData[location + 9] = startingSectorBytes[1]; mbrData[location + 10] = startingSectorBytes[2]; mbrData[location + 11] = startingSectorBytes[3]; //Total Sectors in partition byte[] total = BitConverter.GetBytes(amountOfSectors); mbrData[location + 12] = total[0]; mbrData[location + 13] = total[1]; mbrData[location + 14] = total[2]; mbrData[location + 15] = total[3]; //Boot flag byte[] boot = BitConverter.GetBytes((ushort)0xAA55); mbrData[510] = boot[0]; mbrData[511] = boot[1]; //Save the data Host.WriteBlock(0, 1, ref mbrData); }
public override void ReadBlock(ulong aBlock, byte[] aBuffer) { blockDev.ReadBlock(aBlock + Start, aBuffer); }
public void ReadFatTableSector(UInt64 xSectorNum, byte[] aData) { mDevice.ReadBlock(ReservedSectorCount + xSectorNum, 1, aData); }
public FatFileSystem(BlockDevice aDevice) { mDevice = aDevice; byte[] xBPB = mDevice.NewBlockArray(1); mDevice.ReadBlock(0UL, 1U, xBPB); UInt16 xSig = xBPB.ToUInt16(510); if (xSig != 0xAA55) { throw new Exception("FAT signature not found."); } BytesPerSector = xBPB.ToUInt16(11); SectorsPerCluster = xBPB[13]; BytesPerCluster = BytesPerSector * SectorsPerCluster; ReservedSectorCount = xBPB.ToUInt16(14); NumberOfFATs = xBPB[16]; RootEntryCount = xBPB.ToUInt16(17); TotalSectorCount = xBPB.ToUInt16(19); if (TotalSectorCount == 0) { TotalSectorCount = xBPB.ToUInt32(32); } // FATSz FatSectorCount = xBPB.ToUInt16(22); if (FatSectorCount == 0) { FatSectorCount = xBPB.ToUInt32(36); } //Global.Dbg.Send("FAT Sector Count: " + FatSectorCount); DataSectorCount = TotalSectorCount - (ReservedSectorCount + (NumberOfFATs * FatSectorCount) + ReservedSectorCount); // Computation rounds down. ClusterCount = DataSectorCount / SectorsPerCluster; // Determine the FAT type. Do not use another method - this IS the official and // proper way to determine FAT type. // Comparisons are purposefully < and not <= // FAT16 starts at 4085, FAT32 starts at 65525 if (ClusterCount < 4085) { FatType = FatTypeEnum.Fat12; } else if (ClusterCount < 65525) { FatType = FatTypeEnum.Fat16; } else { FatType = FatTypeEnum.Fat32; } if (FatType == FatTypeEnum.Fat32) { RootCluster = xBPB.ToUInt32(44); } else { RootSector = ReservedSectorCount + (NumberOfFATs * FatSectorCount); RootSectorCount = (RootEntryCount * 32 + (BytesPerSector - 1)) / BytesPerSector; } DataSector = ReservedSectorCount + (NumberOfFATs * FatSectorCount) + RootSectorCount; }
/// <summary> /// Reads a number of blocks from the device /// </summary> /// <param name="aBlockNo">Start block number</param> /// <param name="aBlockCount">Number of blocks</param> /// <param name="aData">Buffer to write to</param> public void ReadBlock(ulong aBlockNo, uint aBlockCount, byte[] aData) { blockDevice.ReadBlock(aBlockNo, aBlockCount, aData); }
private void Initialize() { Console.WriteLine("Start Ext2.Initialize"); mBuffer = new byte[mBackend.BlockSize]; fixed(byte *xBufferAddress = &mBuffer[0]) { mBufferAddress = xBufferAddress; } // first get the superblock; var mBufferAsSuperblock = (SuperBlock *)mBufferAddress; int xAddr = (int)mBufferAddress; Console.WriteLine("Buffer address: " + xAddr); Console.WriteLine("Start reading superblock"); mBackend.ReadBlock(2, mBuffer); Console.WriteLine("End reading"); mSuperblock = *mBufferAsSuperblock; DebugUtil.Send_Ext2SuperBlock(mSuperblock); // read the group descriptors Console.WriteLine("INodeCount: " + mSuperblock.INodesCount); Console.WriteLine("INode#1: " + mBufferAddress[0]); Console.WriteLine("INode#2: " + mBufferAddress[1]); Console.WriteLine("INode#3: " + mBufferAddress[2]); Console.WriteLine("INode#4: " + mBufferAddress[3]); Console.WriteLine("BlockCount: " + mSuperblock.BlockCount); Console.WriteLine("INodesPerGroup: " + (int)mSuperblock.INodesPerGroup); if (mSuperblock.INodesPerGroup == 0x4000) { Console.WriteLine("INodesPerGroup has correct value!"); } uint xGroupDescriptorCount = mSuperblock.INodesCount / mSuperblock.INodesPerGroup; mGroupDescriptors = new GroupDescriptor[xGroupDescriptorCount]; var xDescriptorPtr = (GroupDescriptor *)mBufferAddress; Console.WriteLine("Process GroupDescriptors: " + xGroupDescriptorCount); //Console.ReadLine(); for (int i = 0; i < xGroupDescriptorCount; i++) { Console.WriteLine("Processing GroupDescriptor " + i); uint xATABlock; if (BlockSize == 1024) { xATABlock = (BlockSize * 2) / mBackend.BlockSize; } else { xATABlock = (BlockSize) / mBackend.BlockSize; } xATABlock += (uint)(i / 16); if ((i % 16) == 0) { Console.WriteLine("Read new GroupDescriptorBlock"); mBackend.ReadBlock(xATABlock, mBuffer); Console.WriteLine("End Read"); } mGroupDescriptors[i] = xDescriptorPtr[i % 16]; Console.WriteLine("End of GroupDescriptor check"); } Console.WriteLine("Send GroupDescriptors to log"); DebugUtil.Send_Ext2GroupDescriptors(mGroupDescriptors); }
public override void ReadBlock(ulong aBlock, byte[] aBuffer) { mBackend.ReadBlock(GetActualBlock(aBlock), aBuffer); }
public void Read(ulong aBlockNo, ulong aBlockCount, byte *aData) { host.ReadBlock(aBlockNo, aBlockCount, aData); }