PrivateRegionHelper.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. namespace DiskAccessLibrary.LogicalDiskManager
  11. {
  12. public class PrivateRegionHelper
  13. {
  14. public static long FindUnusedSector(PrivateHeader privateHeader, TOCBlock tocBlock)
  15. {
  16. return FindUnusedRegion(privateHeader, tocBlock, 1);
  17. }
  18. public static long FindUnusedRegion(PrivateHeader privateHeader, TOCBlock tocBlock, int sectorCount)
  19. {
  20. bool[] bitmap = GetPrivateRegionUsageBitmap(privateHeader, tocBlock);
  21. // Reserve the first, second, third, third-last and second-last sectors for TOCBlocks
  22. bitmap[0] = true;
  23. bitmap[1] = true;
  24. bitmap[2] = true;
  25. bitmap[privateHeader.PrivateRegionSizeLBA - 3] = true;
  26. bitmap[privateHeader.PrivateRegionSizeLBA - 2] = true;
  27. int startIndex = 0;
  28. int freeCount = 0;
  29. for (int index = 0; index < bitmap.Length; index++)
  30. {
  31. if (bitmap[index] == false) // free
  32. {
  33. if (freeCount == 0)
  34. {
  35. startIndex = index;
  36. }
  37. freeCount++;
  38. if (freeCount == sectorCount)
  39. {
  40. return (long)privateHeader.PrivateRegionStartLBA + startIndex;
  41. }
  42. }
  43. else
  44. {
  45. freeCount = 0;
  46. }
  47. }
  48. return -1;
  49. }
  50. // On disks with 512-byte sectors Windows will reserve sector 0 and alternate between sectors 1 and 2 of the private region.
  51. // On disks with 4KB sectors Windows will alternate between sectors 0 and 1.
  52. public static long FindUnusedLBAForPrimaryToc(PrivateHeader privateHeader, TOCBlock tocBlock)
  53. {
  54. bool[] bitmap = GetPrivateRegionUsageBitmap(privateHeader, tocBlock);
  55. for (int index = 0; index < bitmap.Length; index++)
  56. {
  57. if (bitmap[index] == false)
  58. {
  59. return (long)privateHeader.PrivateRegionStartLBA + index;
  60. }
  61. }
  62. return -1;
  63. }
  64. // The secondary TOC is usually alternated between the third-last and the second-last sectors of the private region.
  65. public static long FindUnusedLBAForSecondaryToc(PrivateHeader privateHeader, TOCBlock tocBlock)
  66. {
  67. bool[] bitmap = GetPrivateRegionUsageBitmap(privateHeader, tocBlock);
  68. for (int index = bitmap.Length - 1; index >= 0; index--)
  69. {
  70. if (bitmap[index] == false)
  71. {
  72. return (long)privateHeader.PrivateRegionStartLBA + index;
  73. }
  74. }
  75. return -1;
  76. }
  77. private static bool[] GetPrivateRegionUsageBitmap(PrivateHeader privateHeader, TOCBlock tocBlock)
  78. {
  79. // usage bitmap:
  80. bool[] bitmap = new bool[privateHeader.PrivateRegionSizeLBA];
  81. bitmap[privateHeader.PrimaryPrivateHeaderLBA] = true;
  82. bitmap[privateHeader.SecondaryPrivateHeaderLBA] = true;
  83. bitmap[privateHeader.PrimaryTocLBA] = true;
  84. bitmap[privateHeader.SecondaryTocLBA] = true;
  85. foreach (TOCRegion region in tocBlock.Regions)
  86. {
  87. for (int index = 0; index < (int)region.SizeLBA; index++)
  88. {
  89. bitmap[(int)region.StartLBA + index] = true;
  90. }
  91. }
  92. return bitmap;
  93. }
  94. }
  95. }