BasicDiskHelper.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Copyright (C) 2014-2016 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using Utilities;
  11. namespace DiskAccessLibrary
  12. {
  13. public class BasicDiskHelper
  14. {
  15. /// <summary>
  16. /// While technically a partition on an MBR disk could start on sector 1, we use sector 64 for alignment purposes.
  17. /// Note: Windows 7 will start MBR partitions on sector 128 for disks with 512-byte sectors, and on sector 64 for disks with 1KB/2KB/4KB sectors.
  18. /// Windows will start the dynamic data partition of a dynamic disks on sector 63 (or 1 in some cases), but the volume (extent) itself may be aligned to native sector boundaries.
  19. /// </summary>
  20. public const int MBRDiskFirstUsableSector = 64;
  21. public static List<Partition> GetPartitions(Disk disk)
  22. {
  23. List<Partition> result = new List<Partition>();
  24. MasterBootRecord mbr = MasterBootRecord.ReadFromDisk(disk);
  25. if (mbr != null)
  26. {
  27. if (!mbr.IsGPTBasedDisk)
  28. {
  29. for (int index = 0; index < mbr.PartitionTable.Length; index++)
  30. {
  31. PartitionTableEntry entry = mbr.PartitionTable[index];
  32. if (entry.SectorCountLBA > 0)
  33. {
  34. long size = entry.SectorCountLBA * disk.BytesPerSector;
  35. MBRPartition partition = new MBRPartition(entry.PartitionType, disk, entry.FirstSectorLBA, size);
  36. result.Add(partition);
  37. }
  38. }
  39. }
  40. else
  41. {
  42. List<GuidPartitionEntry> entries = GuidPartitionTable.ReadEntriesFromDisk(disk);
  43. if (entries != null)
  44. {
  45. foreach (GuidPartitionEntry entry in entries)
  46. {
  47. GPTPartition partition = new GPTPartition(entry.PartitionGuid, entry.PartitionTypeGuid, entry.PartitionName, disk, (long)entry.FirstLBA, (long)(entry.SizeLBA * (uint)disk.BytesPerSector));
  48. result.Add(partition);
  49. }
  50. }
  51. }
  52. }
  53. return result;
  54. }
  55. public static Partition GetPartitionByStartOffset(Disk disk, long firstSector)
  56. {
  57. List<Partition> partitions = BasicDiskHelper.GetPartitions(disk);
  58. foreach (Partition partition in partitions)
  59. {
  60. if (partition.FirstSector == firstSector)
  61. {
  62. return partition;
  63. }
  64. }
  65. return null;
  66. }
  67. public static List<DiskExtent> GetUnallocatedExtents(Disk disk)
  68. {
  69. MasterBootRecord mbr = MasterBootRecord.ReadFromDisk(disk);
  70. List<DiskExtent> result = new List<DiskExtent>();
  71. if (mbr == null)
  72. {
  73. result.Add(new DiskExtent(disk, 0, disk.Size));
  74. return result;
  75. }
  76. else
  77. {
  78. long dataRegionStartSector;
  79. long dataRegionSize;
  80. if (!mbr.IsGPTBasedDisk)
  81. {
  82. dataRegionStartSector = MBRDiskFirstUsableSector;
  83. dataRegionSize = Math.Min(disk.Size, UInt32.MaxValue * disk.BytesPerSector) - dataRegionStartSector;
  84. }
  85. else
  86. {
  87. GuidPartitionTableHeader gptHeader = GuidPartitionTableHeader.ReadFromDisk(disk);
  88. dataRegionStartSector = (long)gptHeader.FirstUsableLBA;
  89. dataRegionSize = (long)(gptHeader.LastUsableLBA - gptHeader.FirstUsableLBA + 1) * disk.BytesPerSector;
  90. }
  91. List<Partition> partitions = GetPartitions(disk);
  92. List<DiskExtent> usedExtents = new List<DiskExtent>();
  93. foreach (Partition partition in partitions)
  94. {
  95. usedExtents.Add(partition.Extent);
  96. }
  97. return DiskExtentsHelper.GetUnallocatedExtents(disk, dataRegionStartSector, dataRegionSize, usedExtents);
  98. }
  99. }
  100. }
  101. }