Esempio n. 1
0
        protected bool ReadBootSector()
        {
            valid = false;

            if (blockSize != 512)               // only going to work with 512 sector sizes (for now)
            {
                return(false);
            }

            BinaryFormat bootSector = new BinaryFormat(partition.ReadBlock(0, 1));

            byte bootSignature = bootSector.GetByte(BootSector.ExtendedBootSignature);

            if ((bootSignature != 0x29) && (bootSignature != 0x28))
            {
                return(false);
            }

            //TextMode.Write ("EOM NAME: ");

            //for (uint i = 0; i < 8; i++)
            //    TextMode.WriteChar (bootsector.GetByte (BootSector.EOMName + i));

            //TextMode.WriteLine ();

            bytesPerSector    = bootSector.GetUShort(BootSector.BytesPerSector);
            sectorsPerCluster = bootSector.GetByte(BootSector.SectorsPerCluster);
            reservedSectors   = bootSector.GetByte(BootSector.ReservedSectors);
            nbrFats           = bootSector.GetByte(BootSector.FatAllocationTables);
            rootEntries       = bootSector.GetUShort(BootSector.MaxRootDirEntries);

            uint sectorsPerFat16 = bootSector.GetUShort(BootSector.SectorsPerFAT);
            uint sectorsPerFat32 = bootSector.GetUInt(BootSector.FAT32_SectorPerFAT);
            uint totalSectors16  = bootSector.GetUShort(BootSector.TotalSectors);
            uint totalSectors32  = bootSector.GetUInt(BootSector.FAT32_TotalSectors);
            uint sectorsPerFat   = (sectorsPerFat16 != 0) ? sectorsPerFat16 : sectorsPerFat32;
            uint fatSectors      = nbrFats * sectorsPerFat;

            clusterSizeInBytes = sectorsPerCluster * blockSize;
            rootDirSectors     = (((rootEntries * 32) + (bytesPerSector - 1)) / bytesPerSector);
            firstDataSector    = reservedSectors + (nbrFats * sectorsPerFat) + rootDirSectors;

            if (totalSectors16 != 0)
            {
                totalSectors = totalSectors16;
            }
            else
            {
                totalSectors = totalSectors32;
            }

            dataSectors              = totalSectors - (reservedSectors + (nbrFats * sectorsPerFat) + rootDirSectors);
            totalClusters            = dataSectors / sectorsPerCluster;
            entriesPerSector         = (bytesPerSector / 32);
            firstRootDirectorySector = reservedSectors + fatSectors;
            dataAreaStart            = firstRootDirectorySector + rootDirSectors;
            fatStart = reservedSectors;

            if (totalClusters < 4085)
            {
                fatType = FATType.FAT12;
            }
            else if (totalClusters < 65525)
            {
                fatType = FATType.FAT16;
            }
            else
            {
                fatType = FATType.FAT32;
            }

            if (fatType == FATType.FAT12)
            {
                reserved   = 0xFF0;
                last       = 0x0FF8;
                bad        = 0x0FF7;
                fatMask    = 0xFFFFFFFF;
                fatEntries = sectorsPerFat * 3 * blockSize / 2;
            }
            else if (fatType == FATType.FAT16)
            {
                reserved   = 0xFFF0;
                last       = 0xFFF8;
                bad        = 0xFFF7;
                fatMask    = 0xFFFFFFFF;
                fatEntries = sectorsPerFat * blockSize / 2;
            }
            else               //  if (type == FatType.FAT32) {
            {
                reserved   = 0xFFF0;
                last       = 0x0FFFFFF8;
                bad        = 0x0FFFFFF7;
                fatMask    = 0x0FFFFFFF;
                fatEntries = sectorsPerFat * blockSize / 4;
            }

            // some basic checks

            if ((nbrFats == 0) || (nbrFats > 2))
            {
                valid = false;
            }
            else if (totalSectors == 0)
            {
                valid = false;
            }
            else if (sectorsPerFat == 0)
            {
                valid = false;
            }
            else if (!((fatType == FATType.FAT12) || (fatType == FATType.FAT16)))             // no support for Fat32 yet
            {
                valid = false;
            }
            else
            {
                valid = true;
            }

            if (valid)
            {
                base.volumeLabel = bootSector.GetString(fatType != FATType.FAT32 ? BootSector.VolumeLabel : BootSector.FAT32_VolumeLabel, 11);
                base.serialNbr   = bootSector.GetBytes(fatType != FATType.FAT32 ? BootSector.IDSerialNumber : BootSector.FAT32_IDSerialNumber, 4);
            }

            return(valid);
        }