DynamicDisk.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Copyright (C) 2014 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 DiskAccessLibrary;
  11. namespace DiskAccessLibrary.LogicalDiskManager
  12. {
  13. // While DynamicDisk is just a Disk, this class was created to avoid the need to repeatedly read the PrivateHeader and TOCBlock
  14. public class DynamicDisk
  15. {
  16. private Disk m_disk;
  17. private PrivateHeader m_privateHeader;
  18. private TOCBlock m_tocBlock;
  19. public DynamicDisk(Disk disk, PrivateHeader privateHeader, TOCBlock tocBlock)
  20. {
  21. m_disk = disk;
  22. m_privateHeader = privateHeader;
  23. m_tocBlock = tocBlock;
  24. }
  25. public Disk Disk
  26. {
  27. get
  28. {
  29. return m_disk;
  30. }
  31. }
  32. public PrivateHeader PrivateHeader
  33. {
  34. get
  35. {
  36. return m_privateHeader;
  37. }
  38. }
  39. public TOCBlock TOCBlock
  40. {
  41. get
  42. {
  43. return m_tocBlock;
  44. }
  45. }
  46. public Guid DiskGuid
  47. {
  48. get
  49. {
  50. return PrivateHeader.DiskGuid;
  51. }
  52. }
  53. public int BytesPerSector
  54. {
  55. get
  56. {
  57. return Disk.BytesPerSector;
  58. }
  59. }
  60. public static DynamicDisk ReadFromDisk(Disk disk)
  61. {
  62. if (IsDynamicDisk(disk))
  63. {
  64. PrivateHeader privateHeader = PrivateHeader.ReadFromDisk(disk);
  65. if (privateHeader != null)
  66. {
  67. TOCBlock tocBlock = TOCBlock.ReadFromDisk(disk, privateHeader);
  68. if (tocBlock != null)
  69. {
  70. return new DynamicDisk(disk, privateHeader, tocBlock);
  71. }
  72. }
  73. }
  74. return null;
  75. }
  76. public static bool IsDynamicDisk(Disk disk)
  77. {
  78. MasterBootRecord mbr = MasterBootRecord.ReadFromDisk(disk);
  79. if (mbr != null)
  80. {
  81. if (mbr.PartitionTable[0].PartitionType == (byte)PartitionTypeName.DynamicData)
  82. {
  83. return true;
  84. }
  85. else if (mbr.IsGPTBasedDisk)
  86. {
  87. List<GuidPartitionEntry> entries = GuidPartitionTable.ReadEntriesFromDisk(disk);
  88. if (entries != null)
  89. {
  90. if (GuidPartitionEntryCollection.ContainsPartitionTypeGuid(entries, GPTPartition.PrivateRegionPartitionTypeGuid) &&
  91. GuidPartitionEntryCollection.ContainsPartitionTypeGuid(entries, GPTPartition.PublicRegionPartitionTypeGuid))
  92. {
  93. return true;
  94. }
  95. }
  96. }
  97. return false;
  98. }
  99. else
  100. {
  101. // if the disk has no master boot record, it can be a dynamic disk if it has a valid PrivateHeader at sector 6
  102. PrivateHeader privateHeader = PrivateHeader.ReadFromDiskStart(disk);
  103. return (privateHeader != null);
  104. }
  105. }
  106. }
  107. }