LUNStructure.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Copyright (C) 2012-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 SCSI
  12. {
  13. /// <summary>
  14. /// Eight byte logical unit number structure from SAM-2
  15. /// </summary>
  16. public struct LUNStructure
  17. {
  18. public const int Length = 8;
  19. public const int SingleLevelAddressingLimit = 16384;
  20. private ushort FirstLevelAddressing;
  21. private ushort SecondLevelAddressing;
  22. private ushort ThirdLevelAddressing;
  23. private ushort FourthLevelAddressing;
  24. public LUNStructure(ushort lun)
  25. {
  26. FirstLevelAddressing = GetFirstLevelAddressing(lun);
  27. SecondLevelAddressing = 0;
  28. ThirdLevelAddressing = 0;
  29. FourthLevelAddressing = 0;
  30. }
  31. public LUNStructure(byte[] buffer, int offset)
  32. {
  33. FirstLevelAddressing = BigEndianConverter.ToUInt16(buffer, offset + 0);
  34. SecondLevelAddressing = BigEndianConverter.ToUInt16(buffer, offset + 2);
  35. ThirdLevelAddressing = BigEndianConverter.ToUInt16(buffer, offset + 4);
  36. FourthLevelAddressing = BigEndianConverter.ToUInt16(buffer, offset + 6);
  37. }
  38. public byte[] GetBytes()
  39. {
  40. byte[] buffer = new byte[Length];
  41. BigEndianWriter.WriteUInt16(buffer, 0, FirstLevelAddressing);
  42. BigEndianWriter.WriteUInt16(buffer, 2, SecondLevelAddressing);
  43. BigEndianWriter.WriteUInt16(buffer, 4, ThirdLevelAddressing);
  44. BigEndianWriter.WriteUInt16(buffer, 6, FourthLevelAddressing);
  45. return buffer;
  46. }
  47. public AddressingMethod AddressingMethod
  48. {
  49. get
  50. {
  51. return (AddressingMethod)(FirstLevelAddressing >> 14);
  52. }
  53. }
  54. public bool IsSingleLevelLUN
  55. {
  56. get
  57. {
  58. if (AddressingMethod == AddressingMethod.PeripheralDeviceAddressing)
  59. {
  60. byte bus = (byte)((FirstLevelAddressing >> 8) & 0x3F);
  61. return (bus == 0);
  62. }
  63. else if (AddressingMethod == AddressingMethod.FlatSpaceAddressing)
  64. {
  65. return true;
  66. }
  67. return false;
  68. }
  69. }
  70. public ushort SingleLevelLUN
  71. {
  72. get
  73. {
  74. AddressingMethod addressing = this.AddressingMethod;
  75. if (addressing == AddressingMethod.PeripheralDeviceAddressing)
  76. {
  77. byte bus = (byte)((FirstLevelAddressing >> 8) & 0x3F);
  78. if (bus == 0)
  79. {
  80. return (byte)(FirstLevelAddressing & 0xFF);
  81. }
  82. }
  83. else if (addressing == AddressingMethod.FlatSpaceAddressing)
  84. {
  85. return (ushort)(FirstLevelAddressing & 0x3FFF);
  86. }
  87. throw new Exception("Not a single level LUN address");
  88. }
  89. set
  90. {
  91. if (value > SingleLevelAddressingLimit)
  92. {
  93. throw new ArgumentException("Cannot address more than 16384 logical units using single level addressing");
  94. }
  95. FirstLevelAddressing = GetFirstLevelAddressing(value);
  96. SecondLevelAddressing = 0;
  97. ThirdLevelAddressing = 0;
  98. FourthLevelAddressing = 0;
  99. }
  100. }
  101. public static ushort GetPeripheralDeviceAddressing(byte bus, byte lun)
  102. {
  103. if (bus > 63)
  104. {
  105. throw new ArgumentException("Cannot address more than 64 buses using peripheral device addressing");
  106. }
  107. return (ushort)((byte)AddressingMethod.PeripheralDeviceAddressing << 14 | bus << 8 | lun);
  108. }
  109. public static ushort GetFirstLevelAddressing(ushort lun)
  110. {
  111. if (lun > SingleLevelAddressingLimit)
  112. {
  113. throw new ArgumentException("Cannot address more than 16384 logical units using single level addressing");
  114. }
  115. if (lun <= 255)
  116. {
  117. return GetPeripheralDeviceAddressing(0, (byte)lun);
  118. }
  119. else
  120. {
  121. return (ushort)((byte)AddressingMethod.FlatSpaceAddressing << 14 | lun);
  122. }
  123. }
  124. public static implicit operator ushort(LUNStructure structure)
  125. {
  126. if (structure.IsSingleLevelLUN)
  127. {
  128. return structure.SingleLevelLUN;
  129. }
  130. else
  131. {
  132. throw new NotImplementedException("Unsupported LUN structure");
  133. }
  134. }
  135. public static implicit operator LUNStructure(ushort LUN)
  136. {
  137. return new LUNStructure(LUN);
  138. }
  139. }
  140. }