PartitionTableEntry.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 Utilities;
  11. namespace DiskAccessLibrary
  12. {
  13. public class PartitionTableEntry
  14. {
  15. public const int Length = 16;
  16. public byte Status;
  17. public CHSAddress FirstSectorCHS;
  18. public byte PartitionType;
  19. public CHSAddress LastSectorCHS;
  20. public uint FirstSectorLBA;
  21. public uint SectorCountLBA;
  22. public PartitionTableEntry()
  23. {
  24. FirstSectorCHS = new CHSAddress();
  25. LastSectorCHS = new CHSAddress();
  26. }
  27. public PartitionTableEntry(byte[] buffer, int offset)
  28. {
  29. Status = buffer[offset + 0x00];
  30. FirstSectorCHS = new CHSAddress(buffer, offset + 0x01);
  31. PartitionType = buffer[offset + 0x04];
  32. LastSectorCHS = new CHSAddress(buffer, offset + 0x05);
  33. FirstSectorLBA = LittleEndianConverter.ToUInt32(buffer, offset + 0x08);
  34. SectorCountLBA = LittleEndianConverter.ToUInt32(buffer, offset + 0x0C);
  35. }
  36. public void WriteBytes(byte[] buffer, int offset)
  37. {
  38. buffer[offset + 0x00] = Status;
  39. FirstSectorCHS.WriteBytes(buffer, offset + 0x01);
  40. buffer[offset + 0x04] = PartitionType;
  41. LastSectorCHS.WriteBytes(buffer, offset + 0x05);
  42. LittleEndianWriter.WriteUInt32(buffer, offset + 0x08, FirstSectorLBA);
  43. LittleEndianWriter.WriteUInt32(buffer, offset + 0x0C, SectorCountLBA);
  44. }
  45. public PartitionTypeName PartitionTypeName
  46. {
  47. get
  48. {
  49. return (PartitionTypeName)PartitionType;
  50. }
  51. set
  52. {
  53. PartitionType = (byte)value;
  54. }
  55. }
  56. public bool IsBootable
  57. {
  58. get
  59. {
  60. return (Status == 0x80);
  61. }
  62. set
  63. {
  64. Status |= 0x80;
  65. }
  66. }
  67. public bool IsValid
  68. {
  69. get
  70. {
  71. return (Status == 0x80 || Status == 0x00);
  72. }
  73. }
  74. public uint LastSectorLBA
  75. {
  76. get
  77. {
  78. return this.FirstSectorLBA + this.SectorCountLBA - 1;
  79. }
  80. }
  81. }
  82. }