LUNStructure.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 bool IsSingleLevelLUN
  48. {
  49. get
  50. {
  51. return (SecondLevelAddressing == 0) && (ThirdLevelAddressing == 0) && (FourthLevelAddressing == 0);
  52. }
  53. }
  54. public ushort SingleLevelLUN
  55. {
  56. get
  57. {
  58. AddressingMethod addressing = (AddressingMethod)(FirstLevelAddressing >> 14);
  59. if (addressing == AddressingMethod.PeripheralDeviceAddressing)
  60. {
  61. byte bus = (byte)((FirstLevelAddressing >> 8) & 0x3F);
  62. if (bus == 0)
  63. {
  64. return (byte)(FirstLevelAddressing & 0xFF);
  65. }
  66. }
  67. else if (addressing == AddressingMethod.FlatSpaceAddressing)
  68. {
  69. return (ushort)(FirstLevelAddressing & 0x3FFF);
  70. }
  71. throw new Exception("Not a single level LUN address");
  72. }
  73. set
  74. {
  75. if (value > SingleLevelAddressingLimit)
  76. {
  77. throw new ArgumentException("Cannot address more than 16384 logical units using single level addressing");
  78. }
  79. FirstLevelAddressing = GetFirstLevelAddressing(value);
  80. SecondLevelAddressing = 0;
  81. ThirdLevelAddressing = 0;
  82. FourthLevelAddressing = 0;
  83. }
  84. }
  85. public static ushort GetFirstLevelAddressing(ushort LUN)
  86. {
  87. if (LUN > SingleLevelAddressingLimit)
  88. {
  89. throw new ArgumentException("Cannot address more than 16384 logical units using single level addressing");
  90. }
  91. if (LUN <= 255)
  92. {
  93. return LUN;
  94. }
  95. else
  96. {
  97. return (ushort)((byte)AddressingMethod.FlatSpaceAddressing << 14 | LUN);
  98. }
  99. }
  100. public static implicit operator ushort(LUNStructure structure)
  101. {
  102. if (structure.IsSingleLevelLUN)
  103. {
  104. return structure.SingleLevelLUN;
  105. }
  106. else
  107. {
  108. throw new NotImplementedException("Unsupported LUN structure");
  109. }
  110. }
  111. // User-defined conversion from double to Digit
  112. public static implicit operator LUNStructure(ushort LUN)
  113. {
  114. return new LUNStructure(LUN);
  115. }
  116. }
  117. }