Example #1
0
        public Devices()
        {
            int index = 0;

            while (index < 8)
            {
                mDevices[index] = new TapeDevice(index);
                index++;
            }

            while (index < 16)
            {
                mDevices[index] = new DiskDevice(index);
                index++;
            }

            mDevices[CardReaderUnitCode] = new CardReaderDevice(CardReaderUnitCode);
            mDevices[17] = new CardWriterDevice(17);
            mDevices[18] = new PrinterDevice(18);
            Teletype     = new TeletypeDevice(19);
            mDevices[19] = Teletype;
            mDevices[20] = new PaperTapeDevice(20);

            foreach (MixDevice device in mDevices)
            {
                device.ReportingEvent += Device_Reporting;
            }
        }
Example #2
0
        public static Dictionary<int, string> GetVolumesForPhysicalDisk(int deviceNumber)
        {
            StringBuilder volName = new StringBuilder(260);
            IntPtr hVolume = FindFirstVolume(volName, volName.Capacity);
            var result = new Dictionary<int, string>();
            if (hVolume == (IntPtr)(-1))
                return result;
            do
            {
                try
                {
                    using (var dev = new DiskDevice(volName.ToString().TrimEnd('\\'), true))
                    {
                        var dn = dev.QueryDeviceNumber();
                        if (dn == null || dn.DeviceNumber != deviceNumber)
                            continue;
                        result[dn.PartitionNumber] = volName.ToString();
                    }
                }
                catch
                {

                }
            } while (FindNextVolume(hVolume, volName, volName.Capacity));

            FindVolumeClose(hVolume);
            return result;
        }
Example #3
0
        /// <summary>
        /// Initializes the specified disk device.
        /// </summary>
        /// <param name="aDiskDevice">The disk device to initialize.</param>
        public static void InitDisk(DiskDevice aDiskDevice)
        {
            //TODO - Add more partitioning schemes.


            if (InitAsISO9660(aDiskDevice))
            {
#if FSM_TRACE
                BasicConsole.WriteLine("ISO9660 CD/DVD disc detected!");
                BasicConsole.DelayOutput(3);
#endif
            }
            //Must check for GPT before MBR because GPT uses a protective
            //  MBR entry so will be seen as valid MBR.
            else if (InitAsGPT(aDiskDevice))
            {
#if FSM_TRACE
                BasicConsole.WriteLine("GPT formatted disk detected!");
                BasicConsole.DelayOutput(3);
#endif
            }
            else if (!InitAsMBR(aDiskDevice))
            {
                ExceptionMethods.Throw(new FOS_System.Exceptions.NotSupportedException("Non MBR/EBR/GPT/ISO9660 formatted disks not supported."));
            }
        }
Example #4
0
        /// <summary>
        /// Attempts to initialise a disk treating it as MBR formatted.
        /// </summary>
        /// <param name="aDiskDevice">The disk to initialise.</param>
        /// <returns>True if a valid MBR was detected and the disk was successfully initialised. Otherwise, false.</returns>
        private static bool InitAsMBR(DiskDevice aDiskDevice)
        {
#if FSM_TRACE
            BasicConsole.WriteLine("Attempting to read MBR...");
#endif
            byte[] MBRData = new byte[512];
            aDiskDevice.ReadBlock(0UL, 1U, MBRData);
#if FSM_TRACE
            BasicConsole.WriteLine("Read potential MBR data. Attempting to init MBR...");
#endif
            MBR TheMBR = new MBR(MBRData);

            if (!TheMBR.IsValid)
            {
                return(false);
            }
            else
            {
#if FSM_TRACE
                BasicConsole.WriteLine("Valid MBR found.");
#endif
                ProcessMBR(TheMBR, aDiskDevice);

                return(true);
            }
        }
Example #5
0
 /// <summary>
 /// Processes a valid GUID partition table to initialize its partitions.
 /// </summary>
 /// <param name="aGPT">The GPT to process.</param>
 /// <param name="aDiskDevice">The disk device from which the GPT was read.</param>
 private static void ProcessGPT(GPT aGPT, DiskDevice aDiskDevice)
 {
     for (int i = 0; i < aGPT.Partitions.Count; i++)
     {
         GPT.PartitionInfo aPartInfo = (GPT.PartitionInfo)aGPT.Partitions[i];
         Partitions.Add(new Partition(aDiskDevice, aPartInfo.FirstLBA, aPartInfo.LastLBA - aPartInfo.FirstLBA));
     }
 }
        public EraseConfirmationForm(DeviceEnumerator.DeviceInfo info, DiskDevice dev, long volumeSize)
        {
            InitializeComponent();
            this.info = info;

            lblDevName.Text = info.UserFriendlyName;
            lblDevSize.Text = StringHelpers.FormatByteCount(volumeSize) + "B";

            var num = dev.QueryDeviceNumber();
            if (num == null)
                lblInternalName.Text = "Unknown";
            else
                lblInternalName.Text = string.Format(@"\\.\PHYSICALDRIVE{0}", num.DeviceNumber);

            Dictionary<int, string> volumesByPartitionNumbers = null;

            try
            {
                if (num != null)
                    volumesByPartitionNumbers = VolumeManager.GetVolumesForPhysicalDisk(num.DeviceNumber);
            }
            catch
            {
            	
            }

            var layout = dev.QueryLayoutInformation();
            if (layout != null)
                for (int i = 0; i < layout.PartitionCount; i++ )
                {
                    if (layout.PartitionEntry[i].PartitionType == 0)
                        continue;

                    ListViewItem lvi = new ListViewItem((i + 1).ToString());
                    lvi.SubItems.Add(StringHelpers.FormatByteCount(layout.PartitionEntry[i].StartingOffset) + "B");
                    lvi.SubItems.Add(StringHelpers.FormatByteCount(layout.PartitionEntry[i].PartitionLength) + "B");
                    lvi.SubItems.Add(MapPartitionType(layout.PartitionEntry[i].PartitionType));

                    string volID;
                    bool found = false;
                    if (volumesByPartitionNumbers != null && volumesByPartitionNumbers.TryGetValue(layout.PartitionEntry[i].PartitionNumber, out volID))
                    {
                        volumesByPartitionNumbers.Remove(layout.PartitionEntry[i].PartitionNumber);
                        string mountPoints = VolumeManager.GetVolumeMountPoints(volID, '|');
                        if (mountPoints != null)
                        {
                            lvi.Tag = mountPoints.Split('|')[0];
                            lvi.SubItems.Add(mountPoints.Replace("|", ";  "));
                            found = mountPoints.Length > 0;
                        }
                    }

                    lvi.ImageIndex = found ? 0 : 1;

                    lvPartitions.Items.Add(lvi);
                }
        }
Example #7
0
 private static void ProcessISO9660(Disk.ISO9660 aISO9660, DiskDevice aDiskDevice)
 {
     for (int i = 0; i < aISO9660.VolumeDescriptors.Count; i++)
     {
         Disk.ISO9660.VolumeDescriptor volDescrip = (Disk.ISO9660.VolumeDescriptor)aISO9660.VolumeDescriptors[i];
         if (volDescrip is Disk.ISO9660.PrimaryVolumeDescriptor)
         {
             Partitions.Add(volDescrip);
         }
     }
 }
Example #8
0
 /// <summary>
 /// Determines whether the specified disk has had any valid partitions detected.
 /// </summary>
 /// <param name="disk">The disk to check.</param>
 /// <returns>Whether the specified disk has had any valid partitions detected.</returns>
 public static bool HasPartitions(DiskDevice disk)
 {
     for (int i = 0; i < FOS_System.IO.FileSystemManager.Partitions.Count; i++)
     {
         FOS_System.IO.Partition part = (FOS_System.IO.Partition)FOS_System.IO.FileSystemManager.Partitions[i];
         if (part.TheDiskDevice == disk)
         {
             return(true);
         }
     }
     return(false);
 }
Example #9
0
 /// <summary>
 /// Gets the first partition, if any, of the specified disk.
 /// </summary>
 /// <param name="disk">The disk to get the first partition of.</param>
 /// <returns>The partition or null if none found.</returns>
 public static Partition GetFirstPartition(DiskDevice disk)
 {
     for (int i = 0; i < FOS_System.IO.FileSystemManager.Partitions.Count; i++)
     {
         FOS_System.IO.Partition part = (FOS_System.IO.Partition)FOS_System.IO.FileSystemManager.Partitions[i];
         if (part.TheDiskDevice == disk)
         {
             return(part);
         }
     }
     return(null);
 }
Example #10
0
        /// <summary>
        /// Attempts to initialise a disk treating it as GPT formatted.
        /// </summary>
        /// <param name="aDiskDevice">The disk to initialise.</param>
        /// <returns>True if a valid GPT was detected and the disk was successfully initialised. Otherwise, false.</returns>
        private static bool InitAsGPT(DiskDevice aDiskDevice)
        {
            GPT TheGPT = new GPT(aDiskDevice);

            if (!TheGPT.IsValid)
            {
                return(false);
            }
            else
            {
                ProcessGPT(TheGPT, aDiskDevice);
                return(true);
            }
        }
Example #11
0
 /// <summary>
 /// Instantiates a new instance of <see cref="WindowsDisk"/>.
 /// </summary>
 /// <param name="name">The drive letter.</param>
 public WindowsDisk(string name)
 {
     rootPath = Path.GetPathRoot(Path.GetFullPath(name)).TrimEnd(Path.DirectorySeparatorChar);
     volume   = new DiskDevice(@"\\.\" + rootPath, true);
     try
     {
         DiskDevice.STORAGE_DEVICE_NUMBER devNumber = volume.QueryDeviceNumber();
         disk = new DiskDevice(devNumber.PhysicalDrive, true);
     }
     catch
     {
         volume.Close();
         throw;
     }
 }
Example #12
0
        private static bool InitAsISO9660(DiskDevice aDiskDevice)
        {
            // Must check for ISO9660 only on CD/DVD drives
            if (aDiskDevice is Hardware.ATA.PATAPI)
            {
                Disk.ISO9660 TheISO9660 = new Disk.ISO9660(aDiskDevice);

#if FSM_TRACE
                TheISO9660.Print();
#endif
                ProcessISO9660(TheISO9660, aDiskDevice);

                return(true);
            }

            return(false);
        }
Example #13
0
 /// <summary>
 /// Processes a valid master boot record to initialize its partitions.
 /// </summary>
 /// <param name="anMBR">The MBR to process.</param>
 /// <param name="aDiskDevice">The disk device from which the MBR was read.</param>
 private static void ProcessMBR(MBR anMBR, DiskDevice aDiskDevice)
 {
     for (int i = 0; i < anMBR.NumPartitions; i++)
     {
         MBR.PartitionInfo aPartInfo = anMBR.Partitions[i];
         if (aPartInfo.EBRLocation != 0)
         {
             byte[] EBRData = new byte[512];
             aDiskDevice.ReadBlock(aPartInfo.EBRLocation, 1U, EBRData);
             EBR newEBR = new EBR(EBRData);
             ProcessMBR(newEBR, aDiskDevice);
         }
         else
         {
             Partitions.Add(new Partition(aDiskDevice, aPartInfo.StartSector, aPartInfo.SectorCount));
         }
     }
 }
Example #14
0
 /// <summary>
 /// Initializes a new partition.
 /// </summary>
 /// <param name="aDiskDevice">The disk device on which the partition resides.</param>
 /// <param name="aStartingSector">The sector number at which the partition starts.</param>
 /// <param name="aSectorCount">The number of sectors in the partition.</param>
 public Partition(DiskDevice aDiskDevice, UInt64 aStartingSector, UInt64 aSectorCount)
 {
     TheDiskDevice  = aDiskDevice;
     StartingSector = aStartingSector;
 }
Example #15
0
        private void button3_Click(object sender, EventArgs e)
        {
            DiskDevice dev = null;
            FileStream fs = null;
            try
            {
                if (!File.Exists(txtFileName.Text))
                    throw new Exception("Please select a valid image file");

                if (lvDevices.SelectedItems.Count == 0)
                    throw new Exception("Please select target device");

                var rec = lvDevices.SelectedItems[0].Tag as DiskRecord;
                var info = rec == null ? null : rec.Info;
                if (info == null)
                    throw new Exception("Please select a valid target device");

                string devPath = info.DevicePath;

                dev = new DiskDevice(devPath);
                long volumeSize = 0;
                byte[] result = new byte[8];
                if (dev.DeviceIoControl(IOCTL_DISK_GET_LENGTH_INFO, null, result) == 8)
                    volumeSize = BitConverter.ToInt64(result, 0);
                if (volumeSize <= 0)
                    throw new Exception("Please insert a card into the card reader");

                fs = File.Open(txtFileName.Text, FileMode.Open, FileAccess.Read, FileShare.Read);

                if (fs.Length > volumeSize)
                    throw new Exception(string.Format("The selected media ({0}) is too small for the selected image file {1})", StringHelpers.FormatByteCount(volumeSize), StringHelpers.FormatByteCount(fs.Length)));

                _RegHelper.SetValue("LastImageFile", txtFileName.Text);

                if (new EraseConfirmationForm(info, dev, volumeSize).ShowDialog() != DialogResult.OK)
                    return;

                lblProgress.Text = "Preparing...";
                GUIEnabled = false;
                _AbortWriting = false;
                var ctx = new ThreadContext { dev = dev, fs = fs, devID = info.DeviceID, FileName = txtFileName.Text};
                if (cbResize.Checked)
                {
                    try
                    {
                        var pt = ParsedChangeFile.ReadPartitionTable(ctx.FileName);
                        if (pt != null && pt.Length > 0 && pt[pt.Length - 1].Type == 0x83)
                            ctx.ResizedPartition = pt[pt.Length - 1];
                    }
                    catch { }
                }

                new Thread(WriteThreadBody).Start(ctx);
                fs = null;
                dev = null;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (fs != null)
                    fs.Dispose();
                if (dev != null)
                    dev.Dispose();
            }
        }
Example #16
0
        List<DiskRecord> QueryAllDisks()
        {
            List<DiskRecord> disks = new List<DiskRecord>();
            try
            {
                using (var devenum = new DeviceEnumerator(GUID_DEVINTERFACE_DISK))
                    foreach (var info in devenum.GetAllDevices())
                    {
                        long volumeSize = 0;
                        DiskDevice.STORAGE_HOTPLUG_INFO hotplug = null;
                        DiskDevice.STORAGE_DEVICE_DESCRIPTOR desc = null;
                        try
                        {
                            using (var dev = new DiskDevice(info.DevicePath))
                            {
                                try
                                {
                                    hotplug = dev.QueryHotplugInfo();
                                }
                                catch (Exception ex)
                                {

                                }

                                try
                                {
                                    desc = dev.QueryDeviceDescriptor();
                                }
                                catch
                                {

                                }

                                byte[] result = new byte[8];
                                try
                                {
                                    if (dev.DeviceIoControl(IOCTL_DISK_GET_LENGTH_INFO, null, result) == 8)
                                        volumeSize = BitConverter.ToInt64(result, 0);
                                }
                                catch
                                {

                                }
                            }

                            disks.Add(new DiskRecord { Descriptor = desc, Hotplug = hotplug, Info = info, VolumeSize = volumeSize });
                        }
                        catch { }
                    }
            }
            catch
            {
            	
            }
            return disks;
        }
        public void Apply(DiskDevice dev, ProgressDelegate progress)
        {
            byte[] tmp = null;
            long total = 0, done = 0;
            foreach (var chg in _WriteOps)
                total += chg.SizeInBytes;

            foreach (var chg in _WriteOps)
            {
                if (tmp == null || tmp.Length < chg.SizeInBytes)
                    tmp = new byte[chg.SizeInBytes];

                if (chg.Zero)
                {
                    for (int i = 0; i < chg.SizeInBytes; i++)
                        tmp[i] = 0;
                }
                else
                {
                    _File.Seek((long)chg.OffsetInFile, SeekOrigin.Begin);
                    if (_File.Read(tmp, 0, chg.SizeInBytes) != chg.SizeInBytes)
                        throw new Exception("Failed to read change file at offset " + chg.OffsetInFile);
                }

                dev.SeekAbs((long)chg.OffsetInDevice);
                if (dev.Write(tmp, chg.SizeInBytes) != chg.SizeInBytes)
                    throw new Exception("Failed to write change at offset " + chg.OffsetInDevice);

                done += chg.SizeInBytes;
                if (progress != null)
                    progress(done, total);
            }
        }