Пример #1
0
 /// <summary>
 /// Create new inctanse of the <see cref="Partition"/> class.
 /// </summary>
 /// <param name="aHost">A hosting device.</param>
 /// <param name="aStartingSector">A starting sector.</param>
 /// <param name="aSectorCount">A sector count.</param>
 public Partition(BlockDevice aHost, UInt64 aStartingSector, UInt64 aSectorCount)
 {
     mHost           = aHost;
     mStartingSector = aStartingSector;
     mBlockCount     = aSectorCount;
     mBlockSize      = aHost.BlockSize;
 }
Пример #2
0
 /// <summary>
 /// Create new instance of the <see cref="Partition"/> class.
 /// </summary>
 /// <param name="aHost">A hosting device.</param>
 /// <param name="aStartingSector">A starting sector.</param>
 /// <param name="aSectorCount">A sector count.</param>
 public Partition(BlockDevice aHost, ulong StartingSector, ulong SectorCount)
 {
     Host = aHost;
     this.StartingSector = StartingSector;
     mBlockCount         = SectorCount;
     mBlockSize          = Host.BlockSize;
 }
Пример #3
0
		public Partition(BlockDevice aHost, UInt64 aStartingSector, UInt64 aSectorCount)
		{
			mHost = aHost;
			mStartingSector = aStartingSector;
			mBlockCount = aSectorCount;
			mBlockSize = aHost.BlockSize;
		}
Пример #4
0
        private static void InitAta(BlockDevice.Ata.ControllerIdEnum aControllerID,
            BlockDevice.Ata.BusPositionEnum aBusPosition)
        {
            var xIO = aControllerID == BlockDevice.Ata.ControllerIdEnum.Primary
                ? Cosmos.Core.Global.BaseIOGroups.ATA1
                : Cosmos.Core.Global.BaseIOGroups.ATA2;
            var xATA = new BlockDevice.AtaPio(xIO, aControllerID, aBusPosition);
            if (xATA.DriveType == BlockDevice.AtaPio.SpecLevel.Null)
            {
                return;
            }
            if (xATA.DriveType == BlockDevice.AtaPio.SpecLevel.ATA)
            {
                BlockDevice.BlockDevice.Devices.Add(xATA);
                Ata.AtaDebugger.Send("ATA device with speclevel ATA found.");
            }
            else
            {
                Ata.AtaDebugger.Send("ATA device with spec level " + (int) xATA.DriveType +
                                     " found, which is not supported!");
                return;
            }
            var xMbrData = new byte[512];
            xATA.ReadBlock(0UL, 1U, xMbrData);
            var xMBR = new BlockDevice.MBR(xMbrData);

            if (xMBR.EBRLocation != 0)
            {
                //EBR Detected
                var xEbrData = new byte[512];
                xATA.ReadBlock(xMBR.EBRLocation, 1U, xEbrData);
                var xEBR = new BlockDevice.EBR(xEbrData);

                for (int i = 0; i < xEBR.Partitions.Count; i++)
                {
                    //var xPart = xEBR.Partitions[i];
                    //var xPartDevice = new BlockDevice.Partition(xATA, xPart.StartSector, xPart.SectorCount);
                    //BlockDevice.BlockDevice.Devices.Add(xPartDevice);
                }
            }

            // TODO Change this to foreach when foreach is supported
            Ata.AtaDebugger.Send("Number of MBR partitions found:  " + xMBR.Partitions.Count);
            for (int i = 0; i < xMBR.Partitions.Count; i++)
            {
                var xPart = xMBR.Partitions[i];
                if (xPart == null)
                {
                    Console.WriteLine("Null partition found at idx " + i);
                }
                else
                {
                    var xPartDevice = new BlockDevice.Partition(xATA, xPart.StartSector, xPart.SectorCount);
                    BlockDevice.BlockDevice.Devices.Add(xPartDevice);
                    Console.WriteLine("Found partition at idx " + i);
                }
            }
        }
Пример #5
0
        public static bool IsGPTPartition(BlockDevice aBlockDevice)
        {
            byte[] GPTHeader = new byte[512];
            aBlockDevice.ReadBlock(1, 1, ref GPTHeader);

            ulong signature = BitConverter.ToUInt64(GPTHeader, 0);

            return(signature == EFIParitionSignature);
        }
Пример #6
0
        public MBR(BlockDevice device)
        {
            var aMBR = device.NewBlockArray(1);

            device.ReadBlock(0, 1, ref aMBR);

            ParsePartition(aMBR, 446);
            ParsePartition(aMBR, 462);
            ParsePartition(aMBR, 478);
            ParsePartition(aMBR, 494);
        }
Пример #7
0
        internal static void ScanAndInitPartitions(BlockDevice device)
        {
            if (GPT.IsGPTPartition(device))
            {
                var xGPT = new GPT(device);

                Ata.AtaDebugger.Send("Number of GPT partitions found:");
                Ata.AtaDebugger.SendNumber(xGPT.Partitions.Count);
                int i = 0;
                foreach (var part in xGPT.Partitions)
                {
                    Partition.Partitions.Add(new Partition(device, part.StartSector, part.SectorCount));
                    i++;
                }
            }
            else
            {
                var mbr = new MBR(device);

                if (mbr.EBRLocation != 0)
                {
                    //EBR Detected
                    var xEbrData = new byte[512];
                    device.ReadBlock(mbr.EBRLocation, 1U, ref xEbrData);
                    var xEBR = new EBR(xEbrData);

                    for (int i = 0; i < xEBR.Partitions.Count; i++)
                    {
                        //var xPart = xEBR.Partitions[i];
                        //var xPartDevice = new BlockDevice.Partition(xATA, xPart.StartSector, xPart.SectorCount);
                        //Partition.Partitions.Add(xATA, xPartDevice);
                    }
                }
                int c = 0;
                foreach (var part in mbr.Partitions)
                {
                    Partition.Partitions.Add(new Partition(device, part.StartSector, part.SectorCount));
                    c++;
                }
            }
        }
Пример #8
0
        public GPT(BlockDevice aBlockDevice)
        {
            byte[] GPTHeader = new byte[512];
            aBlockDevice.ReadBlock(1, 1, ref GPTHeader);

            // Start of parition entries
            ulong partEntryStart = BitConverter.ToUInt64(GPTHeader, 72);
            uint  numParitions   = BitConverter.ToUInt32(GPTHeader, 80);
            uint  partSize       = BitConverter.ToUInt32(GPTHeader, 84);

            uint paritionsPerSector = 512 / partSize;

            for (ulong i = 0; i < numParitions / paritionsPerSector; i++)
            {
                byte[] partData = new byte[512];
                aBlockDevice.ReadBlock(partEntryStart + i, 1, ref partData);

                for (uint j = 0; j < paritionsPerSector; j++)
                {
                    ParseParition(partData, j * partSize);
                }
            }
        }
Пример #9
0
        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;

        }
Пример #10
0
        public FatFileSystem(Cosmos.HAL.BlockDevice.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;

        }