Beispiel #1
0
        public static MbrPartitionType GetType(HardDisk hdd, int index)
        {
            MbrHeader h;

            hdd.Get(0, out h);
            return(h.GetPartition(index).Type);
        }
Beispiel #2
0
        private GptHeader LoadHeader(HardDisk hdd, long offset)
        {
            GptHeader ret;

            hdd.Get(offset, out ret); //LBA 1
            if (ret.Signature != EfiMagic)
            {
                throw new Exception("Not a GPT.");
            }
            if (ret.Revision != CurrentRevision)
            {
                throw new Exception("Wrong rev.");
            }
            if (ret.HeaderSize < CurrentHeaderSize)
            {
                throw new Exception("Wrong header size.");
            }
            //TODO: check crc
            if (ret.SizeOfPartitionEntry != ParitionEntrySize)
            {
                throw new Exception("Wrong ParitionEntrySize.");
            }
            //TODO: check partition entry CRC

            if (ret.NumberOfPartitions == 0)
            {
                throw new Exception("No partitions!");
            }

            return(ret);
        }
Beispiel #3
0
        public unsafe VdiHardDisk(HardDisk hdd)
            : base(hdd)
        {
            var headBytes = hdd.ReadBytes(0, sizeof(VdiHeader));
            VdiHeader head = Program.ToStruct<VdiHeader>(headBytes);

            if (head.ImageSig != VdiMagic)
                throw new Exception("Wrong magic.");
            if (head.Version != VdiVersion)
                throw new Exception("Wrong version.");
            if (head.SizeOfHeader != VdiHeadSize)
                throw new Exception("Wrong size.");

            if (head.ImageType != ImageType.Dynamic)
                throw new NotImplementedException("Only dynamic is supported.");

            var dataOffset = head.OffsetData;
            mBlockOffsets = new long[head.BlocksInHdd];
            mBlockSize = (int)head.BlockSize;

            for (long i = 0; i < head.BlocksInHdd; i++)
            {
                uint blockLoc;
                hdd.Get<uint>(head.OffsetBlocks + i * 4, out blockLoc);
                if (blockLoc == ~0u)
                    mBlockOffsets[i] = -1;
                else
                    mBlockOffsets[i] = dataOffset + blockLoc * mBlockSize;
            }
        }
Beispiel #4
0
        public static bool IsMbr(HardDisk hdd)
        {
            MbrHeader h;

            hdd.Get(0, out h);
            return(h.BootSig == MbrHeader.MbrMagic);
        }
Beispiel #5
0
        private static T GetLba <T>(HardDisk hdd, long absoluteLba, long extraOffset) where T : struct
        {
            long byteOffset = absoluteLba * SectorSize + extraOffset;
            T    ret;

            hdd.Get <T>(byteOffset, out ret);
            return(ret);
        }
Beispiel #6
0
        public MbrHardDisk(HardDisk hdd, int partition)
        {
            hdd.Get(0, out mHeader);

            //for now, always assume a GPT partition
            if (!MbrHardDisk.IsMbr(hdd))
            {
                throw new Exception("Expected a MBR hdd.");
            }
            if (MbrHardDisk.GetType(hdd, 0) != MbrPartitionType.GptProtective)
            {
                throw new Exception("Expected a GPT protective MBR entry.");
            }

            mPartition = mHeader.GetPartition(partition);
            Init(hdd, (long)mPartition.FirstSectorLba * MbrHeader.SectorSize, (long)mPartition.NumberOfSectors * MbrHeader.SectorSize);
        }
Beispiel #7
0
        unsafe public VdiHardDisk(HardDisk hdd)
            : base(hdd)
        {
            var       headBytes = hdd.ReadBytes(0, sizeof(VdiHeader));
            VdiHeader head      = Program.ToStruct <VdiHeader>(headBytes);

            if (head.ImageSig != VdiMagic)
            {
                throw new Exception("Wrong magic.");
            }
            if (head.Version != VdiVersion)
            {
                throw new Exception("Wrong version.");
            }
            if (head.SizeOfHeader != VdiHeadSize)
            {
                throw new Exception("Wrong size.");
            }

            if (head.ImageType != ImageType.Dynamic)
            {
                throw new NotImplementedException("Only dynamic is supported.");
            }

            var dataOffset = head.OffsetData;

            mBlockOffsets = new long[head.BlocksInHdd];
            mBlockSize    = (int)head.BlockSize;

            for (long i = 0; i < head.BlocksInHdd; i++)
            {
                uint blockLoc;
                hdd.Get <uint>(head.OffsetBlocks + i * 4, out blockLoc);
                if (blockLoc == ~0u)
                {
                    mBlockOffsets[i] = -1;
                }
                else
                {
                    mBlockOffsets[i] = dataOffset + blockLoc * mBlockSize;
                }
            }
        }
Beispiel #8
0
        public MbrHardDisk(HardDisk hdd, int partition)
        {
            hdd.Get(0, out mHeader);

            //for now, always assume a GPT partition
            if (!MbrHardDisk.IsMbr(hdd))
                throw new Exception("Expected a MBR hdd.");
            if (MbrHardDisk.GetType(hdd, 0) != MbrPartitionType.GptProtective)
                throw new Exception("Expected a GPT protective MBR entry.");

            mPartition = mHeader.GetPartition(partition);
            Init(hdd, (long)mPartition.FirstSectorLba * MbrHeader.SectorSize, (long)mPartition.NumberOfSectors * MbrHeader.SectorSize);
        }
Beispiel #9
0
 public static bool IsMbr(HardDisk hdd)
 {
     MbrHeader h;
     hdd.Get(0, out h);
     return h.BootSig == MbrHeader.MbrMagic;
 }
Beispiel #10
0
 public static MbrPartitionType GetType(HardDisk hdd, int index)
 {
     MbrHeader h;
     hdd.Get(0, out h);
     return h.GetPartition(index).Type;
 }